NSUserDefaults 未保存在表格单元格中

发布于 2025-01-08 01:04:22 字数 1190 浏览 4 评论 0原文

当我将 @"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰ゝ天使的微笑 2025-01-15 01:04:22

首先,NSUserDefaults 的一个实例足以完成所有三项修改。要立即将更改“推送”到文件系统,您可以调用[userDefaults Synchronize];。否则无法保证您的更改会立即保存。 Apple 建议仅当您无法等待保存更改时才调用此方法。

- (IBAction)saveButton:(id)sender {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:titleTextfield.text forKey:@"titletext"];
    [userDefaults setObject:detailstextfield.text forKey:@"details"];
    [userDefaults setInteger:selectedCategory forKey:@"category"];
    [userDefaults synchronize];
}

在您的 viewDidLoad: 方法中,尝试输出保存的值以测试应用程序的完整性:

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"details"]);

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.

- (IBAction)saveButton:(id)sender {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:titleTextfield.text forKey:@"titletext"];
    [userDefaults setObject:detailstextfield.text forKey:@"details"];
    [userDefaults setInteger:selectedCategory forKey:@"category"];
    [userDefaults synchronize];
}

In your viewDidLoad: method, try to output the saved values to test your application's integrity:

NSLog(@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"details"]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文