NSUserDefaults只在首次启动时显示窗口 [英] NSUserDefaults to present window only on first launch
问题描述
我试图获得一个窗口显示(它成功做到了)首次启动,在该窗口上有一个按钮(为测试目的),设置bool firstLaunch = NO。
i am trying to get a window to show (which it successfully does) on first launch, there is a button on that window (for testing purposes) that sets the bool firstLaunch = NO. for some reason, after pressing the button, the view is not dismissed.
应用程式代表
userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:firstLaunch forKey:@"firstLaunch"];
newViewController = [[NewView alloc]init];
newViewController.appDelegateOutlet = self;
[userDefaults synchronize];
if ([userDefaults boolForKey:@"firstLaunch"]) {
NSLog(@"first launch");
[_window addSubview:newViewController.view];
[userDefaults synchronize];
}
if (![userDefaults boolForKey:@"firstLaunch"]) {
[userDefaults synchronize];
NSLog(@"not first launch");
按钮
-(IBAction)dismissFirstLaunch:(id)sender{
NSLog(@"%@",appDelegateOutlet);
appDelegateOutlet.firstLaunch = NO;
[appDelegateOutlet.userDefaults setBool:appDelegateOutlet.firstLaunch forKey:@"firstLaunch"];
[appDelegateOutlet.userDefaults synchronize];
NSLog(@"%@",[appDelegateOutlet.userDefaults objectForKey:@"firstLaunch"]);
}
固定:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:firstLaunch forKey:@"firstLaunch"];
newViewController = [[NewView alloc]init];
newViewController.appDelegateOutlet = self;
[userDefaults synchronize];
if ([userDefaults boolForKey:@"firstLaunch"]) {
NSLog(@"YE BOY!!");
[_window addSubview:newViewController.view];
[userDefaults synchronize];
}
else
[self continueLaunch];
return YES;
}
推荐答案
但是没有一行代码在你的按钮的动作,以告诉它关闭视图?这不是psychic - 它会知道如何解除视图,除非你告诉它!
Maybe I'm being blind but there's not a line of code in your button's action to tell it to dismiss the view? It's not psychic - how would it know to dismiss the view unless you tell it to!
PS你也将遇到问题的逻辑,以确定它是否第一次 - 我敢打赌,视图每次都显示:)
PS You're also going to run into problems with your logic to determine if it's the first run - I bet the view is being shown every time :)
这篇关于NSUserDefaults只在首次启动时显示窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!