用NSUserDefaults记录标签栏的下标

将key定义为宏

#define VC_Index_KEY @"kobe"

遵守代理。

// 取值

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

NSInteger index = [[userDefaults objectForKey:VC_Index_KEY] integerValue];

self.selectedIndex = index;

#pragma mark - 标签栏的代理方法


// 选中,存储

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

NSInteger index = [tabBarController.viewControllers indexOfObject:viewController];

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults setObject:[NSNumber numberWithInteger:index] forKey:VC_Index_KEY];

[userDefaults synchronize];

}

你可能感兴趣的:(用NSUserDefaults记录标签栏的下标)