NSUserDefaults问题 [英] NSUserDefaults problem

查看:112
本文介绍了NSUserDefaults问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用委托中有这个 applicationDidFinishLaunching 方法:

I have this in my app delegate applicationDidFinishLaunching method:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults objectForKey:@"AcceptTC"] == nil){
    NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"NO" forKey:@"AcceptTC"];
    [defaults registerDefaults:appDefaults];
}

我在RootViewController中有这个 viewDidLoad 方法:

and I have this in my RootViewController viewDidLoad method:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if(![defaults boolForKey:@"AcceptTC"]){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notice" message:@"By using this application you agree to be bound by the Terms and Conditions as stated within this application." delegate:self cancelButtonTitle:@"No Deal" otherButtonTitles:@"I Understand",nil];
    [alert show];
    [alert release];
}

我的警报视图代表执行此操作:

and my alert view delegate does this:

if(buttonIndex == 0){
            exit(0);
        }
        else{
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:@"YES" forKey:@"AcceptTC"];
        }

然而,当我点击我理解(按钮索引1)然后重启应用程序我仍然看到警报视图!即使我确定将值设置为YES。

However when I click "I understand" (button index 1) and then restart the application I still see the alert view! Even though I've definiely set the value to YES.

我不知道如何更改它。 :(我只希望它在用户第一次启动应用程序时显示 - 我不想在每次使用它时都继续显示它。

I have no idea how to change this. :( I only want it to show the first time a user starts the application - i don't want to keep showing it every time they want to use it.

谢谢

推荐答案

Concering registerDefaults

Concering registerDefaults:


注册域的内容未写入磁盘;每次应用程序启动时都需要调用此方法。您可以放置​​一个plist文件在应用程序的Resources目录中并调用registerDefaults:使用从该文件读入的内容。

The contents of the registration domain are not written to disk; you need to call this method each time your application starts. You can place a plist file in the application's Resources directory and call registerDefaults: with the contents that you read in from that file.



// Load default defaults
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary \
    dictionaryWithContentsOfFile:[[NSBundle mainBundle] \ 
    pathForResource:@"Defaults" ofType:@"plist"]]];

代码取自这个SO答案

另一篇关于 NSDefaults的博客文章

  • http://retrodreamer.com/blog/2010/07/slight-change-of-plan/

这篇关于NSUserDefaults问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆