开发者

Saving the date selected when UIDatePicker exiting

开发者 https://www.devze.com 2023-04-01 05:16 出处:网络
I am trying to load back the date which the user last selected every time my view appears. This IBAction is invoked when my datePicker changes value. datepick is my datePicker

I am trying to load back the date which the user last selected every time my view appears.

This IBAction is invoked when my datePicker changes value. datepick is my datePicker

I am doing this to save to it's rootVC property.

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

Load it everytime the 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;

}

Before pushing dateViewController I pass the saved date(self.selectedDate) to the dateViewController.dateToSet property

- (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];

}

I guess it has got something with how I init the property? When Should I init them?


Try to set parentViewController of datePicker when creating it:

dateViewController.parentViewController = self;

And in method viewWillDisappear do following:

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

In BookingViewController you should declare and synthesize property NSDate *selectedData.

But for this purposes more correct is to use protocol+delegate approach.


Every time you load the view (viewDidLoad), you're re-setting the value to "now". In viewDidLoad you shouldn't initialize choice.


Since I am passing back up the hierarchy, I should not create a new object of the class.

So instead, I should get back the object that was already created as such,

At DateViewController.m (child view)

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

At BookingViewController.m (parent view)

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

Then set it everytime during viewDidLoad at DateViewController.m

if (self.dateToSet == nil) {
    [datepick setDate:minDate animated:YES];
}
else {
    [datepick setDate:dateToSet animated:YES];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消