UITableViewController with MKMapView subview

Discussion in 'iPhone/iPad Programming' started by Scott90, May 16, 2011.

  1. macrumors 6502

    Joined:
    Jul 14, 2008
    #1
    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.

    Code:
    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?
     
  2. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    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.
     
  3. thread starter macrumors 6502

    Joined:
    Jul 14, 2008
    #3
    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.
     
  4. macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #4
    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?
     
  5. thread starter macrumors 6502

    Joined:
    Jul 14, 2008
    #5
    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 :)
     
  6. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    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?
     
  7. thread starter macrumors 6502

    Joined:
    Jul 14, 2008
    #7
    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. The interesting part starts around the fiftieth minute.
     
  8. macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #8
    Your link leads to the entire series of classes. Which class number are you referring to?
     
  9. thread starter macrumors 6502

    Joined:
    Jul 14, 2008
    #9
    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".
     

Share This Page