How to save the last tab in UITabBarController

Discussion in 'iOS Programming' started by guydor, Mar 30, 2010.

  1. guydor macrumors member

    Joined:
    Mar 10, 2009
    #1
    Hi,

    I want to open the last tab the user left in UITabBarController.
    I used this code but it didn't work:

    Code:
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    	tabBarController.selectedIndex =
        [[NSUserDefaults standardUserDefaults] integerForKey:@"lastOpenedTab"];
    	
    }
    
    
    - (void)applicationWillTerminate:(UIApplication *)application {
    	NSUserDefaults *lastTab = [NSUserDefaults standardUserDefaults];
    	[lastTab setInteger:tabBarController.selectedIndex forKey:@"lastOpenedTab"];
    	
    }
    
    
    I'd like to get help

    Thanks!
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Try putting the restore code in viewWillAppear in the tab view controller...
     
  3. guydor thread starter macrumors member

    Joined:
    Mar 10, 2009
    #3
    Didn't work...

    ...
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Then start putting some effort in yourself: log the status of variables (are they nil), log the index you are getting back from the user defaults, check that the tab bar actually has that many tabs (for example if you add them dynamically you may have not done that yet).

    Exhaust all the possibilities and you will find the answer.
     

Share This Page