0

我无法保存到一个变量letsMeet.startTimeLabel。在选择 NSLog 后立即显示正确的值,但是,在我保存到另一个变量 (letsMeet.endTimeLabel) 后,letsMeet.startTimeLabel 更改为 (NULL)。下面是代码:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
letsMeet = (LetsMeet *) [NSEntityDescription insertNewObjectForEntityForName:@"LetsMeet" inManagedObjectContext:managedObjectContext];
switch (actionSheet.tag)
{
    case 1:
    {
        if (buttonIndex == 0)
        {
            UIDatePicker *startDatePicker = (UIDatePicker *)[actionSheet viewWithTag:kDatePickerTag1];

            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"dd"];
            NSDate *selectedDate = [startDatePicker date];

            NSDateFormatter *dayFormatter = [[NSDateFormatter alloc] init];
            [dayFormatter setDateFormat:@"EEEE"];
            NSDate *selectedDay= [startDatePicker date];

            NSDateFormatter *monthFormatter = [[NSDateFormatter alloc] init];
            [monthFormatter setDateFormat:@"MMMM"];
            NSDate *selectedMonth = [startDatePicker date];

            NSString *date = [[NSString alloc] initWithFormat:@"%@", [dateFormatter stringFromDate:selectedDate]];
            DateLabel.text = date;
            [letsMeet setDateLabel:date];

            NSString *month = [[NSString alloc] initWithFormat:@"%@", [dayFormatter stringFromDate:selectedMonth]];
            MonthLabel.text = month;
            [letsMeet setMonthLabel:month];

            NSString *day = [[NSString alloc] initWithFormat:@"%@", [monthFormatter stringFromDate:selectedDay]];
            DayLabel.text = day;
            [letsMeet setDateLabel:day];

            NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
            [timeFormatter setDateFormat: @"h:mm a"];
            NSDate *selectedStartTime = [startDatePicker date];

            NSString *startTime = [[NSString alloc] initWithFormat:@"%@", [timeFormatter stringFromDate:selectedStartTime]];
            StartTimeLabel.text = startTime;
            [letsMeet setStartTimeLabel:startTime];

            NSError *error = nil;
            if (![managedObjectContext save:&error]){

                NSLog(@"Error Saving");
            }
        }
        NSLog (@"This is the StartTime after selecting %@", letsMeet.startTimeLabel);
    }
    break;

    case 2:
    {
        if (buttonIndex == 0)
        {
            UIDatePicker *endTimePicker = (UIDatePicker *)[actionSheet viewWithTag:kDatePickerTag2];
            NSDateFormatter *endTimeFormatter = [[NSDateFormatter alloc] init];
            [endTimeFormatter setDateFormat: @"h:mm a"];
            NSDate *endSelectedTime = [endTimePicker date];
            NSString *endTime = [[NSString alloc] initWithFormat:@"%@", [endTimeFormatter stringFromDate:endSelectedTime]];
            EndTimeLabel.text = endTime;
            [letsMeet setEndTimeLabel:endTime];
            NSLog (@"This is the EndTime %@", letsMeet.endTimeLabel);
            NSLog (@"This is the StartTime after selecting BOTH %@", letsMeet.startTimeLabel);
        }
        else if (buttonIndex == 1)
        {
            EndTimeLabel.text = @"Whenever";
            [letsMeet setEndTimeLabel:EndTimeLabel.text];
        }
        NSError *error = nil;
        if (![managedObjectContext save:&error]) {
    }

}break;

    // Handle the error.
}

}


-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController *destinationViewController = segue.destinationViewController;
    NSLog (@"Prepare For Segue StartTime %@", letsMeet.startTimeLabel);
    NSLog (@"Prepare For Segue EndTime%@", letsMeet.endTimeLabel);
}

这是日志:

2013-02-20 21:38:24.253 AppointmentTime[3129:c07] This is the StartTime after selecting 9:30 AM
2013-02-20 21:38:32.325 AppointmentTime[3129:c07] This is the EndTime 12:15 PM
2013-02-20 21:38:32.325 AppointmentTime[3129:c07] This is the StartTime after Selecting BOTH (null)
2013-02-20 21:38:34.069 AppointmentTime[3129:c07] Prepare For Segue StartTime (null)
2013-02-20 21:38:34.069 AppointmentTime[3129:c07] Prepare For Segue EndTime12:15 PM

问:为什么letsMeet.startTimeLabel第一次显示正确,选择EndTime后,它变为NULL。请注意,EndTime 一直显示正确的值,直到 prepareForSegue。诡异的!

4

1 回答 1

1

根据您的日志和代码,您输入了两次 switch 块。这意味着您要输入该actionSheet:clickedButtonAtIndex:方法两次。所以每次输入方法

letsMeet = (LetsMeet *) [NSEntityDescription insertNewObjectForEntityForName:@"LetsMeet" inManagedObjectContext:managedObjectContext];

语句执行两次,依次创建两个对象。您可以通过从商店取货来查看这一点。

因此,您正在检查两个不同对象中的属性,因此null.

如果您只使用一个托管对象,您可以nil在执行之前添加对对象的检查insertNewObjectForEntityForName:inManagedObjectContext:。这将确保您使用的是相同的对象。
如果您同时使用多个对象,请使用对象 ID 或某个唯一键来识别您的对象并对其进行操作。

编辑:
您可以使用以下代码检查 nil:

if(letsMeet==Nil){
    letsMeet = (LetsMeet *) [NSEntityDescription insertNewObjectForEntityForName:@"LetsMeet" inManagedObjectContext:managedObjectContext];
}

仅当您调用该actionSheet:clickedButtonAtIndex:方法的对象始终在内存中时,这才有效。但是由于您正在坚持,您可能希望从商店中获取对象,然后检查是否。的对象。

NSError *error;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"LetsMeet" inManagedObjectContext:managedObjectContext]];
NSArray *objectArray = [managedObjectContext executeFetchRequest:request error:&error]
if(objectArray.count==0){
    letsMeet = (LetsMeet *) [NSEntityDescription insertNewObjectForEntityForName:@"LetsMeet" inManagedObjectContext:managedObjectContext];
}else{
    letsMeet = (LetsMeet *)[objectArray objectAtIndex:0];
}

注意:如果您只需要保留几个变量,那么 core-data 可能有点过头了。改用 NSUserDefaults 并保持简单。

于 2013-02-21T05:53:38.657 回答