-5

我的程序:

我有视图parentViewchildView. childView有一个UITextField。这UITextField是由用户完成的。然后通过 aUITextField.text作为 aNSString传递。Notification 方法中的 表示该值已成功传递给。parentViewNSNotificationCenterNSLogparentView

现在的问题....当我尝试_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 ];


}
4

1 回答 1

3

问题可能_guideDescription是当您尝试在-saveIntoExisted. 你现在正在做

_guideDescription = [[NSString alloc] initWithString:[[notification userInfo] valueForKey:@"selected"]];

这行得通,但我建议:

[_guideDescription retain];

这确保系统不会释放字符串。

于 2013-05-05T14:19:29.727 回答