2009-10-26 102 views
10

我試圖讓我的應用程序記住應用程序退出前最後一次查看哪個選項卡,以便應用程序在下次啓動時打開相同的選項卡。這是iPhone手機功能的功能:我該怎麼做?如何記住UITabBarController中上次選擇的選項卡?

+0

班級的TabBarController的正好被設置爲RememberingTabBarController在這最近的問題看看,太:http://stackoverflow.com/questions/1619478/how-can-i-have-my- iphone-app-start-with-a-specific-screen-showing – 2009-10-26 05:51:54

回答

6

在UITabBar的代表,覆蓋

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

和存儲項目的指數NSUserDefaults的。下次你的應用程序啓動時,從那裏讀取它,並將其設置回被選中。事情是這樣的:

- 首先,您需要設置一個委託爲您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; 
} 
+0

@luvieere對不起,但您能否詳細說明一下示例代碼? – quantum 2009-10-26 05:38:26

+0

當然,我只是做到了。 – luvieere 2009-10-26 06:00:53

+0

@luvieere Ooops,你設置NSNumber而不是Int – Shmidt 2012-04-19 18:50:25

0

每當用戶選擇一個新選項卡時,將選定的選項卡索引存儲在NSUserDefaults首選項中。然後,當應用程序開始備份時,從首選項加載該值並手動選擇該選項卡。

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; 
} 
+0

此代碼不適用於我 – 2014-12-10 08:57:36

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

UPDATE

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 
} 
+0

這不是'UITabBarDelegate',它是'UITabBarControllerDelegate'。 – MoLice 2017-02-14 05:52:49

+0

我編輯了我的答案。 – 2017-02-14 06:24:40

0

這裏是夫特3溶液。在故事板

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() 
    } 
} 
相關問題