NSUserDefaults仅在首次启动时显示窗口

时间:2011-06-23 15:10:28

标签: iphone cocoa-touch cocoa nsuserdefaults

我试图在第一次启动时显示一个窗口(它成功地做了),该窗口上有一个按钮(用于测试目的)设置bool firstLaunch = NO。出于某种原因,按下按钮后,视图不会被解除。

app delegate

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;
}

1 个答案:

答案 0 :(得分:2)

也许我是瞎了但你按钮的动作中没有一行代码告诉它解雇视图?这不是通灵的 - 除非你告诉它,否则如何解除观点呢?

PS你也会遇到你的逻辑问题,以确定它是否是第一次运行 - 我打赌每次都会显示视图:)