PDA

View Full Version : UITableViewController with MKMapView subview




Scott90
May 16, 2011, 06:15 PM
I've been following Stanford's iPhone programming course on iTunes U (which, btw, I can recommend to all beginning iPhone programmers), and in one of the lectures, it is explained that it is possible for a UITableViewController to display a different view than a UITableView. This is what I wanted to implement in my app, but I can't get it to work quite right, even though I pretty much copied what occurred to me was the key to making it work.


self.mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.hidden = YES;

self.tableView.frame = self.view.bounds; // this line doesn't make a difference, I can leave it out and have the same results

self.view = [[UIView alloc] init];

[self.view addSubview:self.tableView];
[self.view addSubview:self.mapView];


That is part of the viewDidLoad of the class that inherits from UITableViewController. I expected to see a UITableView when that view appears, but all I'm getting is a blank screen. When I switch views (from UITableView to MKMapView), the MKMapView correctly comes up, so I don't think there's anything wrong with the piece of code that does that. If I then switch back to UITableView, I get the blank screen again.

Anybody who can point me in the right direction?



PhoneyDeveloper
May 16, 2011, 07:43 PM
AFAIK, UITableViewController requires that its table view is fullscreen. If you want a table view and some other views you need a subclass of UIViewController.

Scott90
May 16, 2011, 07:53 PM
Is that something that changed in 4.2 then maybe? In that lecture, in which 4.1 is being used, the professor uses a UITableViewController within a UINavigationController, and the rightBarButtonItem of that navigation controller had a target action set up that switched between a table view and map view.

Either way it is maybe easier to have my class inherit from UIViewController and have it implement the delegate methods. I'm going to give that a try, but I'm still curious why it works for him, but not for me.

Shawnpk
May 16, 2011, 11:12 PM
Is that something that changed in 4.2 then maybe? In that lecture, in which 4.1 is being used, the professor uses a UITableViewController within a UINavigationController, and the rightBarButtonItem of that navigation controller had a target action set up that switched between a table view and map view.

Either way it is maybe easier to have my class inherit from UIViewController and have it implement the delegate methods. I'm going to give that a try, but I'm still curious why it works for him, but not for me.

If he has a target action set up, then he either has an IBAction somewhere or a line of code with @selector(buttonPressed). Do you have either of those in your code?

Scott90
May 17, 2011, 03:12 AM
Yeah, the rightBarButtonItem of my UINavigationController switches between map view and table view correctly. However, when switching to map view, it just gave me a blank screen. When switching back to table view, the table view appeared as expected, which is why I'm certain the target action setup is correct.

Anyway, I have now changed my code so that it inherits from UIViewController (as opposed to UITableViewController), created an extra property for the tableView, and it now works the way I wanted. Thanks guys :)

PhoneyDeveloper
May 17, 2011, 06:55 AM
AFAIK, it's been this way from the beginning. However, you seem to be saying that self.view is changed between the tableview that the table view controller creates and a mapview. I've never tried anything like wholesale changing of self.view out from under a table view controller. Your code in the first post is different from this though. What is the lecture number or link to the lecture?

Scott90
May 17, 2011, 12:23 PM
AFAIK, it's been this way from the beginning. However, you seem to be saying that self.view is changed between the tableview that the table view controller creates and a mapview. I've never tried anything like wholesale changing of self.view out from under a table view controller. Your code in the first post is different from this though. What is the lecture number or link to the lecture?

What he did was setting self.view to a generic view: [[UIView alloc] initWithFrame:[UIScreen mainscreen].applicationFrame]

Then he added both views to that subview, one of them hidden. Anyway, the link to the particular lecture is here (http://itunes.apple.com/itunes-u/developing-apps-for-ios-hd/id395605774#). The interesting part starts around the fiftieth minute.

Shawnpk
May 17, 2011, 06:54 PM
What he did was setting self.view to a generic view: [[UIView alloc] initWithFrame:[UIScreen mainscreen].applicationFrame]

Then he added both views to that subview, one of them hidden. Anyway, the link to the particular lecture is here (http://itunes.apple.com/itunes-u/developing-apps-for-ios-hd/id395605774#). The interesting part starts around the fiftieth minute.

Your link leads to the entire series of classes. Which class number are you referring to?

Scott90
May 18, 2011, 01:53 AM
Your link leads to the entire series of classes. Which class number are you referring to?

I'm sorry, I copied the link for that particular one, but apparently that's not how it works. I'm talking about lecture 14, titled "Core Location and Map Kit".