NSUserDefaults 未保存在表格单元格中
当我将 @"hello"
替换为标题字符串时,单元格显示为空白,如果我留下 hello,一切正常,包括“detailsstring”。我不知道为什么要这样做,因为详细信息字符串的设置与标题字符串完全相同。 这是代码: 在我的 .m 文件中
@synthesize detailsstring;
@synthesize titlestring;
,它在 .m 中的 .h 中定义相同,我也有这个:
- (void)viewDidLoad{
titlestring =[[NSUserDefaults standardUserDefaults] objectForKey:@"titletext"];
detailsstring = [[NSUserDefaults standardUserDefaults] objectForKey:@"details"];
tabledata = [[NSArray alloc] initWithObjects:@"hello", nil];
tablesubtitles = [[NSArray alloc] initWithObjects:detailsstring, nil];
[super viewDidLoad];
}
现在,我将这些用户默认值保存在另一个控制器中。这是不同视图控制器中的保存按钮操作:
- (IBAction)saveButton:(id)sender {
NSUserDefaults *titletext = [NSUserDefaults standardUserDefaults];
[titletext setObject:titleTextfield.text forKey:@"titletext"];
NSUserDefaults *details = [NSUserDefaults standardUserDefaults];
[details setObject:detailstextfield.text forKey:@"details"];
NSUserDefaults *categoryUser = [NSUserDefaults standardUserDefaults];
[categoryUser setInteger:selectedCategory forKey:@"category"];
}
我做错了什么?
When I replace the @"hello"
with titlestring the table, the cell shows up blank, If i leave the hello, everything works, including the "detailsstring". I don't know why it's doing that since detailsstring is set up exactly the same as titlestring.
here is the code:
in the .m file I have
@synthesize detailsstring;
@synthesize titlestring;
and it's all defined the same in the .h in the .m I have this as well:
- (void)viewDidLoad{
titlestring =[[NSUserDefaults standardUserDefaults] objectForKey:@"titletext"];
detailsstring = [[NSUserDefaults standardUserDefaults] objectForKey:@"details"];
tabledata = [[NSArray alloc] initWithObjects:@"hello", nil];
tablesubtitles = [[NSArray alloc] initWithObjects:detailsstring, nil];
[super viewDidLoad];
}
Now, I'm saving those Userdefaults in another controller. Here is the save button action in a different view controller:
- (IBAction)saveButton:(id)sender {
NSUserDefaults *titletext = [NSUserDefaults standardUserDefaults];
[titletext setObject:titleTextfield.text forKey:@"titletext"];
NSUserDefaults *details = [NSUserDefaults standardUserDefaults];
[details setObject:detailstextfield.text forKey:@"details"];
NSUserDefaults *categoryUser = [NSUserDefaults standardUserDefaults];
[categoryUser setInteger:selectedCategory forKey:@"category"];
}
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,
NSUserDefaults
的一个实例足以完成所有三项修改。要立即将更改“推送”到文件系统,您可以调用[userDefaults Synchronize];
。否则无法保证您的更改会立即保存。 Apple 建议仅当您无法等待保存更改时才调用此方法。在您的
viewDidLoad:
方法中,尝试输出保存的值以测试应用程序的完整性:First of all, one instance of
NSUserDefaults
is sufficient for all three modifications. To 'push' the changes to the file system immediately, you can call[userDefaults synchronize];
. Otherwise there is no guarantee that your changes are saved instantly. Apple suggests only to call this method if you cannot wait for the changes to be saved.In your
viewDidLoad:
method, try to output the saved values to test your application's integrity: