كيف تتذكر علامة التبويب الأخيرة المحددة في UITabBarController؟

StackOverflow https://stackoverflow.com/questions/1623234

  •  06-07-2019
  •  | 
  •  

سؤال

أحاول أن أجعل تطبيقي يتذكر علامة التبويب التي تم عرضها آخر مرة قبل إنهاء التطبيق، بحيث يفتح التطبيق على نفس علامة التبويب عند تشغيله في المرة التالية.هذه هي وظيفة وظيفة هاتف iPhone:كيف يمكنني أن أفعل هذا؟

هل كانت مفيدة؟

المحلول

في تفويض 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، و<م> الباحث القيم لا يمكن إجراء تسلسل مباشرة. عند بدء تشغيل التطبيق مرة أخرى، فإنه سيتم قراءة وتعيين <م> selectedIndex قيمة من الافتراضات:

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

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

   tabBarController.selectedIndex = 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
}

وأنا subclassed 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"];
}

وهنا هو كيف فعلت ذلك. كلا طرق هي في 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;
}

وهنا هو الحل سويفت 3. مجرد تعيين فئة من TabBarController ل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()
    }
}

وتخزين مؤشر علامة التبويب المحددة في تفضيلات NSUserDefaults في كل مرة يقوم المستخدم بتحديد علامة تبويب جديدة. ثم عندما يبدأ التطبيق النسخ الاحتياطي، تحميل تلك القيمة من تفضيلات يدويا وحدد علامة التبويب.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top