保存UIDatePicker退出时选择的日期

发布于 2024-12-02 19:03:56 字数 2427 浏览 1 评论 0原文

我试图在每次视图出现时加载用户上次选择的日期。

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

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

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

每次视图出现时加载它。

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

}

我想它与我初始化属性的方式有关?我应该什么时候初始化它们?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

后知后觉 2024-12-09 19:03:56

尝试在创建时设置 datePickerparentViewController

dateViewController.parentViewController = self;

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

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

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

但为此目的更正确的是使用协议+委托方法。

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.

阪姬 2024-12-09 19:03:56

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

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

帅哥哥的热头脑 2024-12-09 19:03:56

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

因此,我应该取回已经创建的对象,

在 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];
}

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文