保存 UITextfield 信息时出现问题

发布于 2025-01-06 07:19:21 字数 1527 浏览 0 评论 0原文

当我尝试使用 NSUserdefaults 保存 UITextFields 信息时遇到问题(崩溃)。我知道这与我正在使用的数组有关。这是代码:

- (IBAction)generateNumbers 
{
    // release and remove everyting from the name array
    //    [nameArray release];
    nameArray = nil;

    // reinitilize the name array
    nameArray = [[NSMutableArray alloc] init];

    // Loop through the textfields to get the names into the nameArray
    for (int textFieldCount = 0; textFieldCount<[textFieldArray count]; textFieldCount++) {
        [nameArray addObject:[[textFieldArray objectAtIndex:textFieldCount] text]];
    }

    // Randomly sort the names in the array
    [nameArray sortUsingFunction:randomSort context:nil];

    // Add the random names back into the text fields
    for (int textFieldCount = 0; textFieldCount<[textFieldArray count]; textFieldCount++) {
        [[textFieldArray objectAtIndex:textFieldCount] setText:[nameArray     objectAtIndex:textFieldCount]];
    }
}

      int randomSort(id obj1, id obj2, void *context ) {
         // returns random number -1 0 1
       return (arc4random()%3 - 1);
} 

- (void)viewWillAppear:(BOOL)animated {
          [super viewWillAppear:animated];
          [tex1 setText:[[NSUserDefaults standardUserDefaults]         objectForKey:@"storedTextValue48"]];

 - (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
        [[NSUserDefaults standardUserDefaults] setObject:tex1.text forKey:@"storedTextValue48"];

I'm having issues(crashes) when I try to save UITextFields information with NSUserdefaults. I know it has something to do with the array that I'm using. Here is the code:

- (IBAction)generateNumbers 
{
    // release and remove everyting from the name array
    //    [nameArray release];
    nameArray = nil;

    // reinitilize the name array
    nameArray = [[NSMutableArray alloc] init];

    // Loop through the textfields to get the names into the nameArray
    for (int textFieldCount = 0; textFieldCount<[textFieldArray count]; textFieldCount++) {
        [nameArray addObject:[[textFieldArray objectAtIndex:textFieldCount] text]];
    }

    // Randomly sort the names in the array
    [nameArray sortUsingFunction:randomSort context:nil];

    // Add the random names back into the text fields
    for (int textFieldCount = 0; textFieldCount<[textFieldArray count]; textFieldCount++) {
        [[textFieldArray objectAtIndex:textFieldCount] setText:[nameArray     objectAtIndex:textFieldCount]];
    }
}

      int randomSort(id obj1, id obj2, void *context ) {
         // returns random number -1 0 1
       return (arc4random()%3 - 1);
} 

- (void)viewWillAppear:(BOOL)animated {
          [super viewWillAppear:animated];
          [tex1 setText:[[NSUserDefaults standardUserDefaults]         objectForKey:@"storedTextValue48"]];

 - (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
        [[NSUserDefaults standardUserDefaults] setObject:tex1.text forKey:@"storedTextValue48"];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

怪我太投入 2025-01-13 07:19:21

不要忘记使用 synchronize 保存数据时的方法:

[[NSUserDefaults standardUserDefaults] synchronize];

Don't forget to use synchronize method when you are saving data:

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