ios 记住标签的状态

对于iOS平台来说,持久是金。启动应用程序或者是从暂停及中断状态继续执行程序的时候,我们应该把程序状态恢复到用户上一次离开时的这样子,这样做使得用户能够继续操作上次正在操控的内容,并且能令用户界面与上次会话的界面相符。

@implementation AppDelegate
{
    UITabBarController *tabBarController;
}

- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers changed:(BOOL)changed
{
    NSMutableArray *titles = [NSMutableArray array];
    for (UIViewController *vc in viewControllers) {
        [titles addObject:vc.title];
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:titles forKey:@"tabOrder"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSNumber *tabNumber = [NSNumber numberWithInt:tabBarController.selectedIndex];
    [[NSUserDefaults standardUserDefaults] setObject:tabNumber forKey:@"selectedTab"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]];
    NSMutableArray *controllers = [NSMutableArray array];
    NSArray *titles = [[NSUserDefaults standardUserDefaults] objectForKey:@"tabOrder"];
    if (titles) {
        for (NSString *theTitle in titles) {
            UIViewController *vc = [[UIViewController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
            vc.title = theTitle;
            nav.navigationBar.barStyle = UIBarStyleBlackTranslucent;
            [controllers addObject:nav];
        }
    }else{
        for (int i = 0; i<= 10; i++) {
            UIViewController *vc = [[UIViewController alloc] init];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
            vc.title = [NSString stringWithFormat:@"%d",i];
            nav.navigationBar.barStyle = UIBarStyleBlackTranslucent;
            [controllers addObject:nav];
        }
    }
    tabBarController = [[UITabBarController alloc] init];
    tabBarController.tabBar.barTintColor = [UIColor blackColor];
    tabBarController.tabBar.translucent = NO;
    tabBarController.viewControllers = controllers;
    //tabBarController.customizableViewControllers = controllers;
    tabBarController.delegate = self;
    
    NSNumber *tabNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"selectedTab"];
    if (tabNumber) {
        tabBarController.selectedIndex = [tabNumber intValue];
        
    }
    
    _window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _window.tintColor = [UIColor blueColor];
    tabBarController.edgesForExtendedLayout = UIRectEdgeNone;
    _window.rootViewController = tabBarController;
    [_window makeKeyAndVisible];
    return YES;
}
以便把当前的标签顺序以及用户目前所选的标签保存起来,当这些标签有变化时,他也会执行保存的操作。用户启动应用程序之后,这段代码会搜寻上一次的配置,如果找到了,就据此配置标签栏。为了再标签发生变化时做出响应,标签栏的delegate必须宣称自己遵循UITabBarControllerDelegate协议。范例代码用到了协议里的两个委托方法。

应用程序启动时,会寻找上一次的配置信息,以获取用户所定制的标签顺序以及所选中的标签序号。如果找到了这个信息,就用它们来配置标签栏中的标签,并把上次选定的那个标签激活。

此例仅供参考。


你可能感兴趣的:(iOS)