保存UIDatePicker退出时选择的日期

时间:2011-08-31 12:59:09

标签: iphone uidatepicker

我正在尝试加载用户每次看到我的视图时最后选择的日期。

当我的datePicker更改值时,将调用此IBAction。 datepick是我的datePicker

我这样做是为了保存它的rootVC属性。

- (void)viewWillDisappear:(BOOL)animated {
    BookingViewController *bookingVC = [[BookingViewController alloc] init];
    bookingVC.selectedDate = [datepick date];
    [bookingVC release];
}

每次viewAppears时加载它。

- (void)viewWillAppear:(BOOL)animated
{       
    // set datePicker Range.
    NSDate *todaysDate = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setHour:48];
    NSDateComponents *dateComponents2 = [[NSDateComponents alloc] init];
    [dateComponents2 setMinute:30];
    NSDate *maxDate = [gregorian dateByAddingComponents:dateComponents toDate:todaysDate  options:0];
    NSDate *minDate = [gregorian dateByAddingComponents:dateComponents2 toDate:todaysDate  options:0];
    datepick.maximumDate = maxDate;
    datepick.minimumDate = minDate;
    if (self.dateToSet == nil) {
        [datepick setDate:minDate animated:YES];
    }
    else {
        [datepick setDate:dateToSet animated:YES];
    }
    [dateComponents release];
    [dateComponents2 release];
    [gregorian release];
    choice = [datepick date];   
    choiceString = [dateFormatter stringFromDate:choice];
    dateTextfield.text = choiceString;

}

在推送dateViewController之前,我将保存的日期(self.selectedDate)传递给dateViewController.dateToSet属性

- (void)advanceBooking:(id) sender {
    DateViewController *dateViewController= [[DateViewController alloc]
                                           initWithNibName:@"DateViewController" 
                                           bundle:nil];
    if (self.selectedDate == nil) {
        selectedDate = [[NSDate alloc] init];
    }
    dateViewController.dateToSet = self.selectedDate;
    NSLog(@"dateVC.dateToSet :%@ selectedDate:%@", dateViewController.dateToSet, self.selectedDate); // both read as current date though it was the first call? weird.
    dateViewController.hidesBottomBarWhenPushed = YES;
    dateViewController.navigationItem.hidesBackButton = YES;
    [self.navigationController pushViewController:dateViewController animated:YES];
    [dateViewController release];

}

我想它有关于我如何启动该属性的东西?我应该什么时候开始他们?

3 个答案:

答案 0 :(得分:2)

创建parentViewController时尝试设置datePicker

dateViewController.parentViewController = self;

在方法viewWillDisappear中执行以下操作:

((BookingViewController *)self.parentViewController).selectedDate = [datepick date];

BookingViewController中,您应声明并合成属性NSDate *selectedData

但为了这个目的,更正确的是使用协议+委托方法。

答案 1 :(得分:1)

每次加载视图(viewDidLoad)时,都会将值重新设置为“now”。在viewDidLoad中,您不应初始化choice

答案 2 :(得分:0)

由于我正在传递层次结构,我不应该创建该类的新对象。

相反,我应该找回已经创建的对象,

在DateViewController.m(子视图)

NSArray *viewControllers = [self.navigationController viewControllers];
BookingViewController *bookingVC = (BookingViewController *)[viewControllers objectAtIndex:viewControllers.count - 2];
[bookingVC setSelectedDate:[datepick date]];

在BookingViewController.m(父视图)

if (self.selectedDate != nil) {
    dateViewController.dateToSet = self.selectedDate;
}

然后在DateViewController.m

的viewDidLoad期间每次设置它
if (self.dateToSet == nil) {
    [datepick setDate:minDate animated:YES];
}
else {
    [datepick setDate:dateToSet animated:YES];
}