保存UIDatePicker退出时选择的日期
我试图在每次视图出现时加载用户上次选择的日期。
当我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试在创建时设置
datePicker
的parentViewController
:并在方法
viewWillDisappear
中执行以下操作:在
BookingViewController
中,您应该声明并综合属性NSDate *selectedData
。但为此目的更正确的是使用协议+委托方法。
Try to set
parentViewController
ofdatePicker
when creating it:And in method
viewWillDisappear
do following:In
BookingViewController
you should declare and synthesize propertyNSDate *selectedData
.But for this purposes more correct is to use protocol+delegate approach.
每次加载视图 (
viewDidLoad
) 时,都会将该值重新设置为“now”。在viewDidLoad
中,您不应初始化choice
。Every time you load the view (
viewDidLoad
), you're re-setting the value to "now". InviewDidLoad
you shouldn't initializechoice
.由于我正在向上传递层次结构,因此我不应该创建该类的新对象。
因此,我应该取回已经创建的对象,
在 DateViewController.m(子视图)
在 BookingViewController.m(父视图)
然后每次在 DateViewController.m 的 viewDidLoad 期间设置它
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)
At BookingViewController.m (parent view)
Then set it everytime during viewDidLoad at DateViewController.m