0

我正在编写一个程序,将锻炼保存到沙箱中的 plist 中。它在按下按钮后保存并且名称在文本视图中。保存后,它将锻炼添加到 tableview 实现在模拟器中运行良好,但在实际 iPhone 上却不行。有什么建议么?(另外,这是我的第一篇文章,所以请多多包涵。谢谢)

- (NSString *)dataFilePath {
    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:kFileName];
}

- (IBAction)buttonPressed:(id)sender {

    if ([saveNameTextView.text length] == 0) {  //name is not added to the textview for saving
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops =(" message:@"You need to enter a name for the workout" delegate:self cancelButtonTitle:@"OK, I'll add a name" otherButtonTitles: nil];
        [alert show];
    }else { //name is added for saving in the textview
        NSString *filePath = [self dataFilePath];
        NSMutableDictionary *savedWorkout = [[NSMutableDictionary alloc]initWithContentsOfFile:filePath];

        if ([savedWorkout objectForKey:saveNameTextView.text]) { //if the name already exists, alert the user
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops =(" message:@"Name is already taken, please choose another" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
        }else{  //save the workout
            [savedWorkout setObject:self.saveDetails forKey:saveNameTextView.text];
            [savedWorkout writeToFile:[self dataFilePath] atomically:YES];
            NSString* saveConfirm = [[NSString alloc] initWithFormat:@"%@ has been saved",saveNameTextView.text];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Save Complete" message:saveConfirm delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
        }//end if
    }//end if
}
4

0 回答 0