简体   繁体   English

始终是第一响应者的UITextField

[英]UITextField that is always first responder

I have a ViewController that is just a textfield, a next button, and a back button. 我有一个ViewController,它只是一个文本字段,一个下一个按钮和一个后退按钮。 I want the text field to always be editable while having the keyboard always present . 我希望在始终显示键盘的同时,文本字段始终可编辑 I also want to customize the keyboard to be my own, but that will come once I figure this part out. 我也想将键盘自定义为我自己的键盘 ,但是一旦我弄清楚了这部分,就会来。

EDIT: The keyboard in my case will only actually be a keypad with 10 digits and a backspace key 编辑:在我的情况下,键盘实际上只能是具有10位数字和一个退格键的键盘

What is the best way to go about this? 最好的方法是什么? I've been working around with having a UITextField that works with a custom keyboard view, and then make that the first responder when the view loads, but maybe there are better ways. 我一直在尝试使用可与自定义键盘视图一起使用的UITextField,然后在视图加载时使其成为第一响应者,但是也许有更好的方法。

Thanks in advance! 提前致谢!

To make a UITextField always use the keyboard... 要使UITextField始终使用键盘...

In the viewDidAppear or viewWllAppear function do this... viewDidAppearviewWllAppear函数中执行此操作...

[self.textField becomeFirstResponder];

This will make the keyboard appear and the textField respond to the input. 这将使键盘出现,并且textField响应输入。

To dismiss the keyboard you have to run... 要关闭键盘,您必须运行...

[self.textField resignFirstResponder];

As long as you don't run this it will keep keyboard focus. 只要您不运行此命令,它将保持键盘焦点。

In the view controller's viewWillAppear: method you can call becomeFirstResponder on the text field. 在视图控制器的viewWillAppear:方法中,可以在文本字段上调用becomeFirstResponder This will make the keyboard appear automatically when the view controller appears. 当视图控制器出现时,这将使键盘自动出现。 As long as there is no other way to dismiss the keyboard, that is all you need. 只要没有其他解散键盘的方法,这就是您所需要的。

Of course on the iPad there is a button on the keyboard to dismiss it. 当然,在iPad上,键盘上有一个按钮可将其关闭。 If you want to stop that button from working then implement the following delegate method: 如果要停止该按钮的工作,请实现以下委托方法:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return NO;
}

And in case you want to really have your own keyboard. 如果您真的想拥有自己的键盘。 create a view properly sized and beautified ;).. and put in the textFiled's inputView property. 创建一个适当大小和美化的视图;)..并放入textFiled的inputView属性。

textField.inputView = your custom keyboard view textField.inputView = 您的自定义键盘视图

Cheers. 干杯。

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

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