简体   繁体   English

为 uitextfield 切换输入视图

[英]switching inputview for uitextfield

I am making a custom input view for my textfield in textFieldDidBeginEditing method using我正在使用 textFieldDidBeginEditing 方法为我的文本字段制作自定义输入视图

textField.inputView =wView;

There is button using which I want to get back the keyboard instead of my custom input view.有一个按钮,我想用它来取回键盘而不是我的自定义输入视图。 Any idea how to do that.任何想法如何做到这一点。

If there is no other go I wanted to try with resigning textfield as first responder and make it first responder again.如果没有其他方法,我想尝试将文本字段辞去为第一响应者并再次使其成为第一响应者。 But this would really complicate my project.但这确实会使我的项目复杂化。 I wanted to know if there is any other way.我想知道是否还有其他方法。

Things have changed quite a bit, since I wrote this answer way back in 2012, the recommended approach as mentioned by @yoooriii is:事情发生了很大变化,自从我在 2012 年写这个答案以来,@yoooriii 提到的推荐方法是:

myTextField.inputView = nil
myTextField.reloadInputViews()

改用这个:

-reloadInputViews (UIResponder)

The solution that worked for me to switch back to the default keyboard when the textField was already the first responder (keyboard on screen) was set his inputView to nil than call reloadInputViews like this:当 textField 已经是第一响应者(屏幕上的键盘)时,我切换回默认键盘的解决方案将他的 inputView 设置为 nil,而不是像这样调用 reloadInputViews:

textField.inputView = nil;
[textField reloadInputViews];

Hope this helps.希望这可以帮助。

You need to create a inputView that is not nil.您需要创建一个非 nil 的 inputView。 As Apple help says:正如Apple 帮助所说:

If the value in this property is nil, the text field displays the standard system keyboard when it becomes first responder.如果此属性中的值为 nil,则文本字段在成为第一响应者时显示标准系统键盘。 Assigning a custom view to this property causes that view to be presented instead.将自定义视图分配给此属性会导致显示该视图。

The default value of this property is nil.此属性的默认值为 nil。

To implement it:要实现它:

Create a property like apple says:创建一个像苹果这样的属性说:

@property(readwrite, strong) UIView *inputView;

Initiate it:启动它:

self.inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];

Assign it to your textField or textView's inputView:将其分配给您的 textField 或 textView 的 inputView:

[textView setInputView:self.inputView];

Reload all inputViews:重新加载所有输入视图:

[textView reloadInputViews];

You can do them in textViewDidBeginEditing :您可以在textViewDidBeginEditing执行它们:

- (void)textViewDidBeginEditing:(UITextView *)textView {
      self.inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
      [textView setInputView:self.inputView];
      [textView reloadInputViews];
}

If you're using datePicker inside textfield which has a toolbar in it, do this following thing to get back your input view.如果您在带有工具栏的文本字段中使用 datePicker,请执行以下操作以返回您的输入视图。

Swift 3, 4, 5 :斯威夫特 3、4、5:

 textField.inputView = nil
 textField.inputAccessoryView = nil
 textField.reloadInputViews()
textField.ResignFirstResponder();
textField.inputView = wView;
textField.BecomeFirstResponder();

Try this. 尝试这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM