मैं अपने ऐप को याद रखने की कोशिश कर रहा हूं कि ऐप छोड़ने से पहले कौन सा टैब आखिरी बार देखा जा रहा था, ताकि ऐप लॉन्च होने पर उसी टैब पर खुल जाए। यह आईफोन के फोन फ़ंक्शन की कार्यक्षमता है: मैं यह कैसे कर सकता हूं?UITabBarController में अंतिम चयनित टैब को कैसे याद रखें?
उत्तर
UITabBar के प्रतिनिधि में, ऊपर लिख
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
और NSUserDefaults में आइटम के सूचकांक की दुकान। अगली बार जब आपका ऐप शुरू होता है, तो इसे वहां से पढ़ें, और इसे चयनित होने के लिए वापस सेट करें। कुछ इस तरह:
-first, आप अपने 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;
}
प्रत्येक बार उपयोगकर्ता एक नया टैब चुनने पर NSUserDefaults प्राथमिकताओं में चयनित टैब इंडेक्स स्टोर करें। फिर जब ऐप बैक अप शुरू होता है, वरीयताओं से उस मान को लोड करें और मैन्युअल रूप से उस टैब का चयन करें।
यहाँ कैसे मैंने किया है। दोनों तरीके एपडिलेगेट में हैं और टैबबार नियंत्रक एक आवृत्ति चर है।
- (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;
}
यह कोड मेरे लिए काम नहीं कर रहा है –
मैं TabBarController और subclassed:
- (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
उद्देश्य सी
- (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
}
यह 'UITabBarDelegate' नहीं है, यह' UITabBarControllerDelegate' है। – MoLice
मैंने अपना जवाब संपादित कर लिया है। –
यहाँ स्विफ्ट 3 समाधान है। बस 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()
}
}
- 1. स्विंग जीयूआई फॉर्म में अंतिम मानों को कैसे याद रखें?
- 2. मुझे स्प्रिंग सुरक्षा में कैसे याद रखें?
- 3. UITableView में डिफ़ॉल्ट रूप से पुनः लोड करने के बाद चयनित पंक्तियों को कैसे याद रखें और उन्हें बनाए रखें?
- 4. ज़ेंड फ्रेमवर्क याद रखें मुझे() मुझे याद नहीं है
- 5. फ्लैश गोपनीयता पॉपअप संवाद में "याद रखें" चेकबॉक्स
- 6. jquery क्लिक (OLD VALUE याद रखें)
- 7. एक "मुझे याद रखें" चेकबॉक्स
- 8. .NET अनुप्रयोगों को "याद रखें" प्राथमिकताएं, आदि?
- 9. डीजेंगो-पंजीकरण "मुझे याद रखें"
- 10. अंतिम टैब
- 11. वसंत याद रखें मैं प्रक्रियाऑटोलोगिनक्यूकी
- 12. FOSUserBundle और मुझे याद रखें
- 13. पक्ष पर WPF टैब नियंत्रण टैब को कैसे रखें
- 14. xmonad वर्कस्पेस लेआउट याद रखें?
- 15. रेल - "मुझे याद रखें" क्षमता
- 16. एक्शनबार को स्टाइल कैसे करें, चयनित टैब पर टैब पृष्ठभूमि
- 17. asp.net "मुझे याद रखें" कुकी
- 18. स्टोरीबोर्ड-आधारित ऐप में UITabBarController में टैब अक्षम/सक्षम करें
- 19. कस्टम UITabBarController
- 20. एक UITabBarController
- 21. JTabbedPane: चयनित टैब
- 22. PHP "मुझे याद रखें" सुरक्षा दोष?
- 23. PHP: मुझे याद रखें और सुरक्षा?
- 24. वसंत सुरक्षा याद रखें मेरी कुकी सत्र
- 25. UITabBarController
- 26. डब्ल्यूपीएफ टैबकंट्रोल चयनित टैब बदलें
- 27. rsync: चयनित लक्ष्य निर्देशिका अनुमतियों को कैसे रखें?
- 28. एक "मुझे याद रखें" कुकी टोकन
- 29. jQuery टैब - नए चयनित इंडेक्स
- 30. UITabBarController के 'चयनित दृश्य नियंत्रक' में परिवर्तन होने पर एक ईवेंट निकाल दिया गया?
में भी अपनी TabBarController के वर्ग सेट इस हाल के प्रश्न पर एक नज़र डालें: http://stackoverflow.com/questions/1619478/how-can-i-have-my- iphone-app-start-with-a-specific-screen-showing –