这听起来很容易。我想检查是否第一次选择了标签。这适用于所有标签。 NSUserDefaults
或任何解决方案应该有效。
答案 0 :(得分:1)
我认为你应该像这样抓住UITabBarControllerDelegate
:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)selectingViewController {
if (![selectingViewController isViewLoaded]) {
//this is the first time we meet
}
return YES;
}
祝你好运!
p / s:确保之前[UIViewController view]
没有呼叫selectingViewController
(例如:调用preload UIWebView
:D)
答案 1 :(得分:1)
在你的UITabBarDelegate中,你可以这样做:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
[[NSUserDefaults standardUserDefaults] setValue:@(YES) forKey:item.title];
[[NSUserDefaults standardUserDefaults] synchronize];
}
然后,当你想看看它是否被选中时,只需使用如下函数查看用户默认值:
- (BOOL)didSelectItemWithTitle:(NSString *)title {
NSNumber *value = [[NSUserDefaults standardUserDefaults] valueForKey:title];
return value != nil;
}
您可以观察UIApplicationDidEnterBackgroundNotification,然后清除所有用户默认值,如果您想在此时重置它。
答案 2 :(得分:0)
使用 NSUserDefaults 与标记组合
,使用名为isFirstRun的方法创建UITabBarItem类别不要忘记为要跟踪的每个UITabBarItem设置标记; e.g:
UITabBarItem *bar1 = [[UITabBarItem alloc] init];
UITabBarItem *bar2 = [[UITabBarItem alloc] init];
UITabBarItem *bar3 = [[UITabBarItem alloc] init];
bar1.tag = 1001;
bar2.tag = 1002;
bar2.tag = 1003;
if([bar1 isFirstRun]) {
//to something
[bar1 setDidRun:YES];
}
<强> UITabBarItem + FirstRun.h 强>
#import "UITabBarItem.h"
@interface UITabBarItem (FirstRun)
- (BOOL) isFirstRun;
- (void) setDidRun:(BOOL) flag;
@end
<强> UITabBarItem + FirstRun.m 强>
#import "UITabBarItem+FirstRun.h"
@implementation UITabBarItem (FirstRun)
- (BOOL) isFirstRun {
return return ![[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"tab%ldDidRun", self.tag]];
}
- (void) setDidRun:(BOOL) flag {
[[NSUserDefaults standardUserDefaults] setBool:flag forKey:[NSString stringWithFormat:@"tab%ldDidRun", bar.tag]];
}