Automatically load a ViewController in a UINavigationController within a UITabBar app

Discussion in 'iOS Programming' started by isthisonetaken, Sep 18, 2012.

  1. isthisonetaken
    Expand Collapse
    macrumors regular

    Jun 29, 2006
    Hey Guys,

    so I have a tab bar app, mostly up and running. Within it, I have a tab that initially loads a tableview of information where a tap on a cell loads a detail view. There is a button in the Nav Bar of the table view controller that loads a map view controller which shows the same information on a map. I also have settings for the app, and currently have one for automatically loading the map instead of the table. I save the BOOL value to NSUSerDefaults and can load that value within the table view to see if I should continue on or load the map.

    This is where I'm running into problems. If I put something into the viewDidLoad, it works great, but only once. So if I start the app (with the map set to default in the settings) and click the tab, it loads the map just like I want. If I go back tot he table, then some other tab and come back, it doesn't load the map (because the vc is still loaded in memory). If I put the same call into the viewDid/WillAppear methods, I run into a problem where it loads the map just fine, but when I tap the back button to go back to the table, it pushes me right back into the map view (because it's running it's check and seeing it should load the map).

    Currently, I have a BOOL shouldShowMap which gets set to whatever is in the NSUSerDefaults in viewDidLoad. Then in viewWillAppear, I check to see if it's set to YES and if so, I perform the segue to show the map. Then in my viewWillDisappear I check to see how many VCs are in the nag controllers viewArrays property and it it's just 1 (so just the table view) I set the shouldShowMap to whatever is in NSUSerDefaults, if it's more than 1, I check to see if the second VC is the map and if so set it to NO.

    Doing it that way is leading to some problems though. First of all, if I load the table with the setting to OFF, then chenage the setting to ON and go back, it doesn't load the map (because that initial loading set the shouldLoadMap to NO). If I quit the app (with the setting to ON still) and restart it, the map loads properly the first time, however if I go back to the table view then some other tab and come back to the tab, it loads the map within a map (if that makes sense?). So If I hit the back button, to go to the table, it leads me to another map and has these in the log:

    - nested pop animation can result in corrupted navigation bar
    - Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.

    Any ideas of what I'm doing wrong, or a better way to implement this feature?

  2. dejo
    Expand Collapse


    Staff Member

    Sep 2, 2004
    The Centennial State
    I'm a little confused. Do you want the map view to be pushed onto the navigation stack (and, thus, is at a lower "level" than the table view) or do you want the map view to replace the table view in the navigation stack?

    In other words, do you want:

    UITabBarController > UINavigationController > UITableViewController > MKMapView

    or do you want:

    UITabBarController > UINavigationController > UITableViewController
    UITabBarController > UINavigationController > MKMapView
    (depending on BOOL value)?
  3. isthisonetaken
    Expand Collapse
    thread starter macrumors regular

    Jun 29, 2006
    The first one. So that if the user wants the table view they can tap the back button.

Share This Page