【问题标题】:DatePicker via textField.inputView is shown on the second time only (Regular keyboard is called first time)通过 textField.inputView 的 DatePicker 仅在第二次显示(第一次调用常规键盘)
【发布时间】:2016-04-09 10:46:19
【问题描述】:

我在一页上使用了四个文本字段。对于第一个和第二个文本字段,我使用常规键盘。 第三个和第四个是时间值 - 我使用以编程方式创建的 DatePicker 并通过 IBAction 方法中的 textField.inputView 设置它。

关键是我的 DatePicker 仅在第二次显示 - 第一次调用常规键盘。

它只发生一次 - 当我第一次打开页面并单击具有时间值的文本字段时。当我第二次点击此文本字段时 - 它工作正常(显示我的日期选择器)

我该如何解决这个问题?

代码示例:

- (IBAction)wakeUpWeekdaysPressed:(id)sender {
    UIDatePicker *timePicker = [[UIDatePicker alloc] init];
    timePicker.datePickerMode = UIDatePickerModeTime;

    UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44)];
    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc]
                                   initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(weekdaysTimePickerDoneAction:)];
    UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc]
                                     initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(weekdaysTimePickerCancelAction:)];
    UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]
                                      initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [toolbar setItems:@[cancelButton, flexibleSpace, doneButton]];
    // toolbar.barStyle = UIBarStyleBlackTranslucent;
    toolbar.translucent = YES;

    [_wakeUpTimeWeekdays setInputView:timePicker];
    [_wakeUpTimeWeekdays setInputAccessoryView:toolbar];
}

【问题讨论】:

    标签: ios objective-c datepicker keyboard


    【解决方案1】:

    我用另一种方式解决了这个问题。

    在 IBAction 方法中为文本字段初始化 DatePicker。 主要问题的根本原因是我为方法Editing Did End 选择了发送事件类型。所以我将发送事件更改为Editing Did Begin

    现在它工作正常。谢谢。

    【讨论】:

      【解决方案2】:

      只需在 viewdidload 中编写代码而不是此方法即可。

      【讨论】:

      • 没有帮助,现在日期选择器或键盘都不会显示在此文本字段中
      • (IBAction)wakeUpWeekdaysPressed:(id)sender 方法调用任何按钮点击?如果是,那么不要这样做。将此代码粘贴到 viewdidload 中,并且不要 resignfirstresponder。
      • 不需要进行IBaction。您可以在 vieddidload 中轻松完成此设置。这是标准的方式。如果你跟着它,你以后就不会遇到这种问题了!我已经更新了我的答案。
      • 谢谢。您的选择也适用。将此代码保留在单独的 IBAction 方法中的要点是,我需要使用来自 textField 的值填充此 datePicker。还不知道怎么做。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      • 2014-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多