保存UITextfield信息的问题

时间:2012-02-16 18:22:37

标签: objective-c sdk nsmutablearray

当我尝试使用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"];

1 个答案:

答案 0 :(得分:0)

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

[[NSUserDefaults standardUserDefaults] synchronize];