Wie zuletzt ausgewählte Registerkarte in UITabBarController erinnern?
Frage
Ich versuche, meine app erinnern, welche Registerkarte wurde zuletzt Wesen betrachtet zu machen, bevor die App verlassen, so dass der App auf den gleichen Tab öffnet, wenn es nächste gestartet wird. Dies ist die Funktionalität des Telefons Funktion des iPhone: Wie kann ich dies tun
Lösung
In der UITabBar den Delegierten, überschreiben
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
und speichern Artikel 's Index in NSUserDefaults. Das nächste Mal Ihre Anwendung startet, lesen Sie es von dort, und legen Sie es zurück zu ausgewählt werden. So etwas wie folgt aus:
-first, würden Sie einen Delegaten für Ihr UITabBar gesetzt, wie folgt aus:
tabBarController.delegate = anObject;
-in anObject , überschreiben didSelectItem :
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setInteger: [NSNumber numberWithInt: tabBarController.selectedIndex]
forKey:@"activeTab"];
[def synchronize];
}
Beachten Sie, dass Sie eine NSNumber speichern, als int Werte können nicht direkt serialisiert werden. Wenn Sie die App erneut starten, liest es und stellen Sie die selectedIndex Wert von den Standardeinstellungen:
- (void)applicationDidFinishLaunchingUIApplication *)application {
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
int activeTab = [(NSNumber*)[def objectForKey:@"activeTab"] intValue];
tabBarController.selectedIndex = activeTab;
}
Andere Tipps
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
}
I subclassed TabBarController und:
- (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"];
}
Hier ist, wie ich es tat. Beide Methoden sind in der AppDelegate und TabBarController ist eine Instanzvariable.
- (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;
}
Hier ist die Swift 3 Lösung. Stellen Sie einfach die Klasse Ihres TabBarController im 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()
}
}
Speichern Sie den ausgewählten Registerkarte Index in den NSUserDefaults Präferenzen jedes Mal, wenn der Benutzer eine neue Registerkarte auswählt. Dann, wenn die App wieder starten, diesen Wert aus den Einstellungen laden und manuell diese Registerkarte auswählen.