我的节目:
我有查看parentView
和childView
。 childView
有一个UITextField
。此UITextField
由用户完成。然后,UITextField.text
通过NSString
作为parentView
传递给NSNotificationCenter
。通知方法中的NSLog
显示该值已成功传递给parentView
。
现在问题....当我尝试在_guideDescription
我的应用程序崩溃时访问saveIntoExisting
(包含传递的值)。我不明白为什么在能够检索通知方法中的值时崩溃。
(llbs)
没有错误。
任何人都知道为什么会这样?
- (void)viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(notificationSelectedDescription:)
name:@"selectedDescription"
object:nil];
}
- (void) notificationSelectedDescription:(NSNotification *)notification {
_guideDescription = [[notification userInfo] valueForKey:@"selected"];
NSLog(@"Show me: %@",[_guideDescription description]);
}
- (IBAction)createExercise:(UIButton *)sender {
if (![self fetch]) {
NSLog(@"It doesnt exist already");
[self save];
} else {
NSLog(@"It does exist already");
/*
* ADD CODE ON HOW TO ACT WHEN EXISTS
*/
[self saveIntoExisting];
}
}
- (void) saveIntoExisting {
NSLog(@"saveintoExisting 1");
NSLog(@"saveintoExisting show me: %@",[_guideDescription description]);
NSFetchRequest *fetchExercise = [[NSFetchRequest alloc] init];
NSEntityDescription *entityItem = [NSEntityDescription entityForName:@"Exercise" inManagedObjectContext:context];
[fetchExercise setEntity:entityItem];
[fetchExercise setPredicate:[NSPredicate predicateWithFormat:@"exerciseName == %@",_originalExerciseName]];
[fetchExercise setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"User", nil]];
fetchExercise.predicate = [NSPredicate predicateWithFormat:@"user.email LIKE %@",_appDelegate.currentUser];
NSError *error = nil;
NSArray* oldExercise = [[context executeFetchRequest:fetchExercise error:&error] mutableCopy];
Exercise *newExercise = (Exercise*) oldExercise[0];
newExercise.exerciseName = _exerciseName.text;
newExercise.exercisePic = _selectedImage;
if (![_guideDescription isEqualToString:@""]) {
newExercise.exerciseDescription =_guideDescription;
}
if (_youTubeLink){
newExercise.youtube =_youTubeLink;
}
if (![_selectRoutine.titleLabel.text isEqualToString:@"add to routine"]) {
newExercise.routine = [[self getCurrentRoutine] exercise];
[[self getCurrentUser] addRoutines:[[self getCurrentRoutine] exercise]];
}
[[self getCurrentUser] addExercisesObject:newExercise];
error = nil;
[context save:&error ];
}
答案 0 :(得分:3)
当您尝试在_guideDescription
中访问-saveIntoExisted
字符串时,问题可能已解除分配。你现在正在做
_guideDescription = [[NSString alloc] initWithString:[[notification userInfo] valueForKey:@"selected"]];
这有效,但我建议:
[_guideDescription retain];
这可以确保系统不会释放字符串。