【问题标题】:NSUserDefaults to present window only on first launchNSUserDefaults 仅在首次启动时显示窗口
【发布时间】:2011-06-23 15:10:28
【问题描述】:

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

应用委托

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

【问题讨论】:

    标签: iphone cocoa-touch cocoa nsuserdefaults


    【解决方案1】:

    也许我是瞎子,但在您的按钮操作中没有一行代码告诉它关闭视图?它不是通灵的——除非你告诉它,否则它怎么会知道忽略这个观点!

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

    【讨论】:

    • 已编辑代码 - 从 superview 中删除。我会告诉你它是否有效
    • 好吧,这是我的问题,我可以删除视图,但第二个 if 语句永远不会被调用
    • 修复了它。我喜欢你向我暗示而不是直接告诉我。我让应用程序的其余部分完成启动到它自己的名为-(void)continueLaunch 的方法,它在按钮按下时被调用,第二个 if 语句被更改为 else,它也在那里被调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2015-09-20
    • 1970-01-01
    相关资源
    最近更新 更多