objective-c 如何在 UITextField 上自动打开键盘?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2273470/
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
How to open the keyboard automatically on UITextField?
提问by eemceebee
I have a very simple table and when tocuh a cell it opens a new view with one UITextfield. All I want is that the keyboard will automatically opens, without the user have to touch the UITextfield.
我有一个非常简单的表格,当 tocuh 一个单元格时,它会打开一个带有一个 UITextfield 的新视图。我想要的是键盘会自动打开,而无需用户触摸 UITextfield。
Its all done in Interface Builder, so I am not sure how I do this. I guess I need to set the focus at some point ?
这一切都是在 Interface Builder 中完成的,所以我不确定我是如何做到这一点的。我想我需要在某个时候设置焦点?
Thanks
谢谢
回答by jessecurry
To cause the keyboard to show up immediately you'll need to set the text field as the first responder using the following line:
要使键盘立即显示,您需要使用以下行将文本字段设置为第一响应者:
[textField becomeFirstResponder];
You may want to place this in the viewDidAppear:method.
你可能想把它放在viewDidAppear:方法中。
回答by Nico S.
Swift 3 & 4:
斯威夫特 3 和 4:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
textField.becomeFirstResponder()
}
回答by Yash
Prefer adding the first responder on the main thread -
更喜欢在主线程上添加第一响应者 -
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.async {
self.textField.becomeFirstResponder()
}
}
This will come in handy when view controller view is added as a subview.
当视图控制器视图被添加为子视图时,这将派上用场。

