xcode 使用 UIDatePicker 时如何禁用键盘但启用 UITextFields?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16629988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:23:55  来源:igfitidea点击:

How to disable keyboard but enable UITextFields when using UIDatePicker?

iosxcodeuitextfielduidatepicker

提问by SpokaneDude

This one is kind of hard to explain, so ask any questions you need to clarify the question. I have an iPad app (XCode 4.2, iOS 6, ARC and Storyboards).

这个有点难以解释,所以提出任何你需要澄清的问题。我有一个 iPad 应用程序(XCode 4.2、iOS 6、ARC 和 Storyboards)。

In this app, I have a UIPopoverthat contains a UIViewwith two (2) UITextFieldsin it and a UIDatePicker. (see image). When I go to that scene, I have unchecked userInteractionEnabledon both textFields to prevent the keyboard from responding, which works, but doesn't allow the other UITextFieldto be accessed after the first one. I tried [oFinishTime setInputView:oTimePicker];but it takes the timepicker and moves it outside of the popover, which is unacceptable.

在这个应用程序中,我有一个UIPopover包含UIView两 (2) 个UITextFields和一个UIDatePicker. (见图)。当我去那个场景时,我没有选中userInteractionEnabled两个 textFields 以防止键盘响应,这可行,但不允许UITextField在第一个之后访问另一个。我试过了,[oFinishTime setInputView:oTimePicker];但它需要时间选择器并将其移到弹出窗口之外,这是不可接受的。

image showing 2 uiviews and a datepicker

显示 2 个 uiviews 和一个日期选择器的图像

I have touchUpInsideevents "wired" for each of the textFields, but neither one gets called when tapped due to UserInteractionEnabled being unchecked. I could remedy this whole thing if I could have it enabled and set a flag in the action event.

touchUpInside为每个文本字段“连接”了事件,但由于未选中 UserInteractionEnabled,因此在点击时没有一个被调用。如果我可以启用它并在动作事件中设置一个标志,我可以解决整个问题。

How can I enable both UITextFieldsyet prevent the keyboard from showing? I will be happy to post any relevant code you need to see.

我怎样才能同时启用两者UITextFields但防止键盘显示?我很乐意发布您需要查看的任何相关代码。

回答by yunas

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        // return NO to disallow editing.

assign delegate for each textfield and in the delegate method verify if its the one which should open picker instead of keyboard. If it is the one then return NO else YES

为每个文本字段分配委托,并在委托方法中验证它是否应该打开选择器而不是键盘。如果是,则返回 NO else YES

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
      if (textfield == startTimeTextField)
       { 
          [self openPicker];
          return NO;
        }
    return YES;
}

回答by ila

May be I did not understand your problem correctly , but it seems like you all you are trying to do is Open a picker on tap and display the value set in UIPicker. You are not really using any functionality of a UItextField. A label or a button might serve the same purpose and save you from doing the extra work of disabling the keyboard.

可能是我没有正确理解你的问题,但看起来你所做的只是打开一个选择器并显示在 UIPicker 中设置的值。您并没有真正使用 UItextField 的任何功能。标签或按钮可能具有相同的目的,并使您免于执行禁用键盘的额外工作。

In any case if you want to use textfield and disable keyboard picker appearing as the input view and have your own input view to it, you could do

在任何情况下,如果您想使用文本字段并禁用键盘选择器作为输入视图出现并拥有自己的输入视图,您可以这样做

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    [textField resignFirstResponder];
}

You could take a look at this: Display datepicker on tapping on textfield

你可以看看这个: Display datepicker on tap on textfield