我想动态更改 UIDatePicker 的 datePickerMode

发布于 2024-11-03 06:00:27 字数 449 浏览 0 评论 0原文

我希望能够有一个日期选择器可以输入月-日-年-小时-分钟。 我知道两个 UIDatePicker 对象是否是可能的。 例如 UIDatePicker obj1; //对于月-日-年 UIDatePicker obj2; //对于小时-分钟

但我不想有两个UIDatePicker用于一个日期输入:月-日-年-小时-分钟。 所以我做了一个UIDatePicker和一个UIButton。 该按钮用于更改 UIDatePicker 的 datePickerMode。

拾取器的形状可以改变。 [月|日|年] <--> [hh:mm]

但是当选择器的模式发生如下变化时,

[hh:mm] -> [月|日|年] -> [hh:mm]

选择器的小时日期变为 00:00,即使在模式更改之前设置为 11:59。

为什么?

无论模式如何改变,月日年都不会改变。

I want to be able to the one date picker can input month-day-year-hour-minute.
I know if two UIDatePicker objects were it's possible.
e.g.
UIDatePicker obj1; //for month-day-year
UIDatePicker obj2; //for hour-minute

But I don't want to have two UIDatePicker for one date input:month-day-year-hour-minute.
So I made one UIDatePicker and one UIButton.
The button is for change UIDatePicker's datePickerMode.

The picker's figure can be changed. [mm|dd|yyyy] <--> [hh:mm]

But when the picker's mode changes as following,

[hh:mm] -> [mm|dd|yyyy] -> [hh:mm]

the hour-date of the picker becomes 00:00, even if set like 11:59 before the mode changed.

Why?

The month-day-year never changes however the mode is changed.

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

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

发布评论

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

评论(2

世俗缘 2024-11-10 06:00:27

即使在模式更改之前设置为 11:59,选择器的小时日期也会变为 00:00。为什么?

可能是因为日期选择器存储日期的时间设置为午夜(如果处于 mm|dd|yyyy 模式)。因此,您必须将所选时间存储在单独的 NSDate 对象中,并且当用户从 mm|dd|yyyy 切换到 hh:mm 时,将日期选择器的日期设置为正确的日期和时间(通过 NSDateComponents 从一个日期中提取时间和分钟,从另一个日期中提取年/月/日,然后将所有组件组合成一个新日期)。

the hour-date of the picker becomes 00:00, even if set like 11:59 before the mode changed. Why?

Probably because the date picker stores dates with the time set to midnight if it's in mm|dd|yyyy mode. So you would have to store the selected time in a separate NSDate object, and when the user switches from mm|dd|yyyy to hh:mm, set the date picker's date to the correct date and time (by extracting time and minute from the one date and year/month/day from the other via NSDateComponents and compositing a new date with all the components).

水晶透心 2024-11-10 06:00:27

谢谢您的回答。

我在原始代码中添加了 3 行,如下所示。
到目前为止似乎还不错。

    - (IBAction) switchDateTime {
  switch (datePicker_.datePickerMode) {
    case UIDatePickerModeDate: {
      datePicker_.datePickerMode = UIDatePickerModeDateAndTime;
      [swBtn_ setTitle:NSLocalizedString(@"swichBtn_date", @"") forState:UIControlStateNormal];
    } break;
    case UIDatePickerModeDateAndTime: {
      NSDate *tmp = [datePicker_.date copy];//<- added
      datePicker_.datePickerMode = UIDatePickerModeDate;
      datePicker_.date = tmp;               //<- added
      [tmp release];
      [swBtn_ setTitle:NSLocalizedString(@"swichBtn_time", @"") forState:UIControlStateNormal];
    } break;
    default:
      assert(0);
      break;
  }
}

Thank you for your answer.

I added 3 lines in my original codes as following.
It seems to be good so far.

    - (IBAction) switchDateTime {
  switch (datePicker_.datePickerMode) {
    case UIDatePickerModeDate: {
      datePicker_.datePickerMode = UIDatePickerModeDateAndTime;
      [swBtn_ setTitle:NSLocalizedString(@"swichBtn_date", @"") forState:UIControlStateNormal];
    } break;
    case UIDatePickerModeDateAndTime: {
      NSDate *tmp = [datePicker_.date copy];//<- added
      datePicker_.datePickerMode = UIDatePickerModeDate;
      datePicker_.date = tmp;               //<- added
      [tmp release];
      [swBtn_ setTitle:NSLocalizedString(@"swichBtn_time", @"") forState:UIControlStateNormal];
    } break;
    default:
      assert(0);
      break;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文