问题
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?
回答1:
In the UITabBar's Delegate, overwrite
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
and store item's index in NSUserDefaults. Next time your app starts, read it from there, and set it back to being selected. Something like this:
-first, you would set a delegate for your UITabBar, like this:
tabBarController.delegate = anObject;
-in anObject, overwrite didSelectItem:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setInteger: [NSNumber numberWithInt: tabBarController.selectedIndex]
forKey:@"activeTab"];
[def synchronize];
}
Note that you save a NSNumber, as int values cannot be serialized directly. When you start the app again, it will read and set the selectedIndex value from the defaults:
- (void)applicationDidFinishLaunchingUIApplication *)application {
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
int activeTab = [(NSNumber*)[def objectForKey:@"activeTab"] intValue];
tabBarController.selectedIndex = activeTab;
}
回答2:
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
}
回答3:
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"];
}
回答4:
Here's how I did it. Both Methods are in the appDelegate and tabBarController is an instance variable.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
//Remember the users last tab selection
NSInteger tabIndex = self.tabBarController.selectedIndex;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger: tabIndex forKey:@"activeTab"];
if (![userDefaults synchronize])
{
NSLog(@"Error Synchronizing NSUserDefaults");
}
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
//Set the tabBarController to the last visted tab
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
int activeTab = [(NSNumber*)[userDefaults objectForKey:@"activeTab"] intValue];
self.tabBarController.selectedIndex = activeTab;
}
回答5:
Here is the Swift 3 solution. Just set the class of your TabBarController to RememberingTabBarController
in the Storyboard
import Foundation
import UIKit
class RememberingTabBarController: UITabBarController, UITabBarControllerDelegate {
let selectedTabIndexKey = "selectedTabIndex"
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
// Load the last selected tab if the key exists in the UserDefaults
if UserDefaults.standard.object(forKey: self.selectedTabIndexKey) != nil {
self.selectedIndex = UserDefaults.standard.integer(forKey: self.selectedTabIndexKey)
}
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
// Save the selected index to the UserDefaults
UserDefaults.standard.set(self.selectedIndex, forKey: self.selectedTabIndexKey)
UserDefaults.standard.synchronize()
}
}
回答6:
Store the selected tab index in the NSUserDefaults preferences each time the user selects a new tab. Then when the app starts back up, load that value from the preferences and manually select that tab.
来源:https://stackoverflow.com/questions/1623234/how-to-remember-last-selected-tab-in-uitabbarcontroller