Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remember last selected tab in UITabBarController?

I'm trying to make my app remember which tab was last being viewed before the app quit, so that the app opens up to the same tab when it is next launched. This is the functionality of the iPhone's phone function: how can I do this?

like image 369
quantum Avatar asked Oct 26 '09 05:10

quantum


2 Answers

UPDATE

In the UITabBarControllerDelegate's Delegate, overwrite

Objective C

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
NSLog(@"%lu",self.tabBarController.selectedIndex);
return YES;
}

In this delegate method you will get last selected index.

Swift 3.2

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
{
    print("%i",tabBarController.selectedIndex)
    return true
}
like image 70
Nimisha Ranipa Avatar answered Nov 07 '22 07:11

Nimisha Ranipa


I subclassed TabBarController and:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.selectedIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"activeTab"];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [[NSUserDefaults standardUserDefaults] setInteger: self.selectedIndex
             forKey:@"activeTab"];
}
like image 26
Shmidt Avatar answered Nov 07 '22 07:11

Shmidt