如何记住UITabBarController中最后选择的选项卡?

时间:2009-10-26 05:32:09

标签: iphone sdk tabs uitabbar

我正在尝试让我的应用记住在应用退出之前最后一次查看哪个标签,以便应用在下次启动时打开相同的标签。这是iPhone手机功能的功能:我该怎么做?

6 个答案:

答案 0 :(得分:8)

在UITabBar的代表中,覆盖

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item

并在NSUserDefaults中存储 item 的索引。下次您的应用程序启动时,从那里读取它,并将其重新设置为被选中。像这样:

- 首先,你要为你的UITabBar设置一个委托,如:

tabBarController.delegate = anObject;

-in anObject ,覆盖 didSelectItem

       - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
        {
          NSUserDefaults *def = [NSUserDefaults standardUserDefaults];

          [def setInteger: [NSNumber numberWithInt: tabBarController.selectedIndex]
 forKey:@"activeTab"];

          [def synchronize];
        }

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

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

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

   tabBarController.selectedIndex = activeTab;
} 

答案 1 :(得分:5)

<强>更新

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
}

答案 2 :(得分:2)

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

答案 3 :(得分:1)

我是这样做的。 两个方法都在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;
}

答案 4 :(得分:1)

这是Swift 3解决方案。只需将StoryBarController的类设置为Storyboard中的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()
    }
}

答案 5 :(得分:0)

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