[英]NSUserDefaults, saving data issue
So I have an app that lets you put in a title and an author on the front page, then you hit enter and it will go into the actual features of the app. 因此,我有一个应用程序,可让您在首页上输入标题和作者,然后按Enter键,它将进入该应用程序的实际功能。 I want to save the data with NSUserDefaults, so that when they click the enter button, it saves it * AND goes into the next view.
我想保存在NSUserDefaults的数据,这样,当他们按一下回车键,这样可以节省它* 并进入下一个视图。 I have it setup with the storybord already to go into the next view, but when I use this code:
我已经使用storybord对其进行了设置,可以进入下一个视图,但是当我使用此代码时:
-(IBAction)enter:(id)sender {
titleString = [[NSString alloc] initWithFormat:[title text]];
[title setText:titleString];
NSUserDefaults *titleDefault = [NSUserDefaults standardUserDefaults];
[titleDefault setObject:titleString forKey:@"stringkey"];
authorString = [[NSString alloc] initWithFormat:[author text]];
[title setText:authorString];
NSUserDefaults *authorDefault = [NSUserDefaults standardUserDefaults];
[authorDefault setObject:authorString forKey:@"stringkey2"];
}
It will always crash when you hit the button. 当您按下按钮时,它将始终崩溃。 I have all the NSStrings defined and such, so I don't see what the problem is.
我已经定义了所有NSStrings,所以我看不出问题是什么。 I also have it loading with:
我也有它加载:
title.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"stringkey"];
author.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"stringkey2"];
Under the viewdidload, so can I have some help as to why this wouldn't work? 在viewdidload之下,我为什么能不能帮上忙?
Just to optimize your -enter:
method: 只是为了优化您的
-enter:
方法:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:title.text forKey:@"stringkey"];
[defaults setObject:author.text forKey:@"stringkey2"];
[defaults synchronize]; // this will store them instantly!
For the crash: can you please provide us with the exact crash log? 对于崩溃:您能否向我们提供确切的崩溃日志? Maybe your Button is linked to an unknown selector...
也许您的按钮已链接到未知的选择器...
First, there are a few questions about this: here , here . 首先,对此有几个问题: 这里 , 这里 。 Try searching around first, and at least let people know why the duplicate questions you found didn't work for you.
首先尝试搜索,至少让人们知道为什么您发现重复的问题对您不起作用。
Secondly, I think you might benefit from adding a synchronize
. 其次,我认为你可能会从添加受益
synchronize
。 Use this: 用这个:
titleString = [[NSString alloc] initWithFormat:[title text]];
[title setText:titleString];
NSUserDefaults *titleDefault = [NSUserDefaults standardUserDefaults];
[titleDefault setObject:titleString forKey:@"stringkey"];
[titleDefault synchronize];
authorString = [[NSString alloc] initWithFormat:[author text]];
[author setText:authorString];
NSUserDefaults *authorDefault = [NSUserDefaults standardUserDefaults];
[authorDefault setObject:authorString forKey:@"stringkey2"];
[authorDefault synchronize];
I also changed the [title setText:authorString];
我还更改了
[title setText:authorString];
line to [author setText:authorString];
到
[author setText:authorString];
. 。 I do, however, think it is a bit redundant the way you are allocating the
NSString
s to the value of the text fields and then setting the text back to the value of the string you created. 但是,我确实认为将
NSString
分配给文本字段的值,然后将文本设置回创建的字符串的值有点多余。 I don't get the need. 我没有需要。 Anyway, hope this helps.
无论如何,希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.