0

ワークアウトをサンドボックスの plist に保存するプログラムを書いています。ボタンを押すと保存され、名前がテキストビューに表示されます。保存後、ワークアウトをテーブルビューに追加します。この実装はシミュレーターではうまく機能しますが、実際の 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