uidatepicker奇怪的行为-未设置日期和选择错误的日期 [英] uidatepicker strange behaviour - not setting date and wrong date being selected

查看:79
本文介绍了uidatepicker奇怪的行为-未设置日期和选择错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用UIDatePicker时遇到了一些奇怪的问题。我检查了论坛,但提供的答案并没有带来任何运气。

I'm having a few strange issues with the UIDatePicker. I checked the forums and I haven't had any luck with the answers supplied.

说我选择一个日期为20/11/2010 EN_AUS的语言环境。当我NSLog时,日期属性为2010-11-19 13:00:00 +0000。但是,当我格式化它并在标签中显示它时,它看起来是正确的,即20/11/2010?!

Say I pick a date, 20/11/2010 EN_AUS locale. When I NSLog it the date property is 2010-11-19 13:00:00 +0000. But when I format it and display in a label it appears correct, i.e. 20/11/2010?!

我保存此日期,以便用户下次输入时查看uiDatePicker应该在最后选择的日期自动设置。但这只是没有发生,它仍然显示当前日期。

I save this date so that the next time the user enters this view the uiDatePicker should automatically be set at the last selected date. But it just isn't happening, it still displays the current date.

我在下面列出了重要的代码,任何建议都值得赞赏:)

I've listed the important code below, any advice is appreciated :)

// setting up the uiDatePicker  
NSLocale *ozLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_AUS"];
self.datePicker.calendar = [NSCalendar autoupdatingCurrentCalendar];
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:39600]]; // Melbourne time zone
[self.datePicker setLocale:ozLocale];
self.datePicker.timeZone = [NSTimeZone localTimeZone];
[ozLocale release];

// displaying the date from the uidatePicker
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
self.dateLabel.text = [dateFormatter stringFromDate:self.datePicker.date];
[dateFormatter release];


// In viewWillAppear I set the uiDatePicker. lastDatePicked has the value of 2010-11-19 13:00:00 +0000 at this point
[self.datePicker setDate:self.appDelegate.lastDatePicked animated:FALSE];


推荐答案

首先,请注意 2010-11-19 13:00:00 +0000 采用UTC,因此将EN_AUS格式的日期提前11小时,这就是为什么您看到 20 / 11/2010 使用语言环境设置格式时。因此,这根本不是问题,这是正确的行为。

First of all, note that 2010-11-19 13:00:00 +0000 is in UTC so the date formatted for EN_AUS is going to be ahead by 11 hours which is why you see 20/11/2010 when formatting it using the locale. So that isn't a problem at all and that is the correct behaviour.

我认为您的 setDate:animated:行,因此我将确保 self.appDelegate self.datePicker 实际上不是nil在那条线之上。

I see no problem with your setDate:animated: line so I would make sure that self.appDelegate and self.datePicker are actually non-nil just above that line.

这篇关于uidatepicker奇怪的行为-未设置日期和选择错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆