NSUserDefaults to present window only on first launch

房东的猫 提交于 2019-12-11 08:50:11

问题


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.

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");

button

-(IBAction)dismissFirstLaunch:(id)sender{
NSLog(@"%@",appDelegateOutlet);
appDelegateOutlet.firstLaunch = NO;
[appDelegateOutlet.userDefaults setBool:appDelegateOutlet.firstLaunch forKey:@"firstLaunch"];
[appDelegateOutlet.userDefaults synchronize];
NSLog(@"%@",[appDelegateOutlet.userDefaults objectForKey:@"firstLaunch"]);
}

fixed:

- (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:


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 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 :)



来源:https://stackoverflow.com/questions/6456329/nsuserdefaults-to-present-window-only-on-first-launch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!