Iphone 如何记住UITabBarController中上次选择的选项卡?

Iphone 如何记住UITabBarController中上次选择的选项卡?,iphone,sdk,tabs,uitabbar,Iphone,Sdk,Tabs,Uitabbar,我试图让我的应用程序记住在退出应用程序之前最后一次查看的选项卡,以便下次启动应用程序时打开相同的选项卡。这是iPhone手机功能的功能:我如何才能做到这一点?每次用户选择新选项卡时,将所选选项卡索引存储在NSUserDefaults首选项中。然后,当应用程序启动备份时,从首选项中加载该值并手动选择该选项卡。在UITabBar的委托中,覆盖 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 并将项的索引存储在

我试图让我的应用程序记住在退出应用程序之前最后一次查看的选项卡,以便下次启动应用程序时打开相同的选项卡。这是iPhone手机功能的功能:我如何才能做到这一点?

每次用户选择新选项卡时,将所选选项卡索引存储在NSUserDefaults首选项中。然后,当应用程序启动备份时,从首选项中加载该值并手动选择该选项卡。

在UITabBar的委托中,覆盖

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
并将项的索引存储在NSUserDefaults中。下次启动应用程序时,从那里读取它,并将其设置回被选中状态。大概是这样的:

tabBarController.delegate = anObject;
-首先,您将为UITabBar设置一个委托,如下所示:

tabBarController.delegate = anObject;
-在对象中,覆盖didSelectItem:

请注意,您保存了一个NSNumber,因为int值不能直接序列化。 当您再次启动应用程序时,它将读取并设置默认值中的selectedIndex值:

- (void)applicationDidFinishLaunchingUIApplication *)application {  
   NSUserDefaults *def = [NSUserDefaults standardUserDefaults];

   int activeTab = [(NSNumber*)[def objectForKey:@"activeTab"] intValue];

   tabBarController.selectedIndex = activeTab;
} 
我是这样做的。 这两种方法都在appDelegate中,tabBarController是一个实例变量

- (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;
}

I子类TabBarController和:

- (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"];
}

更新

In the UITabBarControllerDelegate's Delegate, overwrite
目标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
}

以下是Swift 3解决方案。只需在情节提要中将TabBarController的类设置为
RememberingTabBarController

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()
    }
}

@很抱歉,你能用更多的示例代码详细说明一下吗?@luviere Ooops,你设置的是NSNumber而不是Int@luvieere为什么不直接保存
tabbar控制器。在
-[UIApplicationLegate应用程序将终止:
中选择索引?在应用程序终止之前,它不会一直被调用吗?再看看最近的这个问题:这不是
uitabardelegate
,而是
UITabBarControllerDelegate