iPhone iOS 4 UITimePicker强制24小时视图

发布于 2024-12-07 19:19:50 字数 841 浏览 1 评论 0原文

我的应用程序中有一个 UITimePicker,我想用它来选择用户事件后的特定小时数和分钟数。 (例如晚上 7 点后 3 小时 15 分钟)这就是为什么我想以 24 小时格式显示日期,否则用户可能会感到困惑。

有没有办法强制时间选择器显示 24 小时视图,而与区域设置的时钟格式无关?或者我只是选择另一个区域设置?

UIDatePicker *timePicker;
NSDateFormatter *timePickerFormatter;

  [self.view addSubview:timePicker];
    float tempHeight = timePicker.frame.size.height;
    timePicker.frame = CGRectMake(0,150 ,self.view.frame.size.width , tempHeight);
    timePicker.hidden = YES;
    timePickerFormatter= [[NSDateFormatter alloc]init ];
    [timePickerFormatter setDateFormat:@"HH:mm"]; //formats date for processing

谢谢你!

更新:我找到了这样的答案:

timepicker.datePickerMode = UIDatePickerModeCountDownTimer;

这通过显示 0 到 23 小时和 0 到 59 分钟有效地完成了我想要做的事情,无需进一步自定义!抱歉询问 24 小时格式,这就是我要移植的 Android 应用程序正在使用的格式!

I got a UITimePicker in my app, and I want to use it to select a certain number of hours and minutes after a user event. (for example 3 hours 15 minutes after 7PM) This is why I want to show the date in 24 hour format, otherwise the user might get confused.

Is there a way to force the timepicker to show 24 hour view, independent of the clock format for the locale? Or do I just pick another locale?

UIDatePicker *timePicker;
NSDateFormatter *timePickerFormatter;

  [self.view addSubview:timePicker];
    float tempHeight = timePicker.frame.size.height;
    timePicker.frame = CGRectMake(0,150 ,self.view.frame.size.width , tempHeight);
    timePicker.hidden = YES;
    timePickerFormatter= [[NSDateFormatter alloc]init ];
    [timePickerFormatter setDateFormat:@"HH:mm"]; //formats date for processing

Thank you!

Update: I found the answer like this:

timepicker.datePickerMode = UIDatePickerModeCountDownTimer;

This effectively accomplishes what I'm trying to do by displaying 0 to 23 hours and 0 to 59 minutes, no further customization required! Sorry for asking about the 24 hour format, this is what the Android app that I'm porting over was using!

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

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

发布评论

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

评论(2

安穩 2024-12-14 19:19:50

您是否尝试使用 日历UIDatePickerlocale 属性?

这应该是您问题的解决方案,因为正如文档所解释的那样(阅读 UIDatePicker 类参考):

UIDatePickerModeTime

日期选择器显示小时、分钟和(可选)AM/PM 名称。 显示的确切项目及其顺序取决于区域设置。这种模式的一个例子是 [ 6 | 53 | 53下午]。

[编辑] 经过进一步调查,似乎 NSLocale 无法覆盖 AM/PM 设置...(我通过...简单地获得了该信息在 Stackoverflow 中搜索!并且您的问题之前已经被提出并回答过很多次)

Did you try to use the calendar and locale properties of UIDatePicker?

That should be the solution for your problem, because as the documentation explains (read the UIDatePicker class reference):

UIDatePickerModeTime

The date picker displays hours, minutes, and (optionally) an AM/PM designation. The exact items shown and their order depend upon the locale set. An example of this mode is [ 6 | 53 | PM ].

[EDIT] after further investigation, it seems the NSLocale can't override the AM/PM setting… (I had that information by… simply searching here in Stackoverflow! And your question has already been asked and answered many times before)

坦然微笑 2024-12-14 19:19:50

对于可视化,我使用:

[self.datePickerView setLocale:[NSLocale localeWithLocaleIdentifier:@"ru_RU"]];

您可以使用您自己的语言环境。

对于我使用的 NSDateFormatter:

[_dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

它不再破坏我的应用程序。

For visualization I use:

[self.datePickerView setLocale:[NSLocale localeWithLocaleIdentifier:@"ru_RU"]];

You may use your own locale.

And for NSDateFormatter I Use:

[_dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

It is no longer breaks my applications.

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