11

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

4

6 回答 6

9

更新

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.

斯威夫特 3.2

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
{
    print("%i",tabBarController.selectedIndex)
    return true
}
于 2016-10-18T05:38:23.813 回答
8

在 UITabBar 的 Delegate 中,覆盖

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

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

-首先,您将为您的 UITabBar 设置一个委托,如下所示:

tabBarController.delegate = anObject;

-在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;
} 
于 2009-10-26T05:37:14.563 回答
3

我将 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"];
}
于 2012-04-19T19:00:21.340 回答
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;
}
于 2011-04-26T16:54:06.893 回答
1

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

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()
    }
}
于 2017-05-12T15:48:14.220 回答
0

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

于 2009-10-26T05:35:18.657 回答