ios Swift:成为 UITextField 上的第一响应者不工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25353687/
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
Swift: Become First Responder on UITextField Not Working?
提问by blee908
I've created a custom UIViewController with one UITextField on Storyboard. On viewDidLoad
, I set the UITextFIeld to becomeFirstResponder
, nothing happened (no keyboards popped up).
我在 Storyboard 上创建了一个带有一个 UITextField 的自定义 UIViewController。在 上viewDidLoad
,我将 UITextFIeld 设置为becomeFirstResponder
,什么也没发生(没有键盘弹出)。
I then tried calling resignFirstResponder()
, but it returned false. Next I tried to find who the first responder
is through looping all the subviews using the code over @ Get the current first responder without using a private API. It turns out none of the sub views are the first responder.
然后我尝试调用resignFirstResponder()
,但它返回 false。接下来,我试图first responder
通过使用代码在 @Get当前第一响应者而不使用私有 API 上循环所有子视图来查找是谁。事实证明,没有一个子视图是第一响应者。
My ViewController in storyboard
我在故事板中的 ViewController
My Code
我的代码
class BLTestViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var tf: UITextField
override func viewDidLoad() {
super.viewDidLoad()
tf.delegate = self
tf.becomeFirstResponder()
// Do any additional setup after loading the view.
}
}
As simple as it gets...WHY ISN'T THE KEYBOARD COMING UP!!! One thing I do have on is auto layout, but I'm not sure if that affects the keyboard popping up.
就这么简单……为什么键盘没有出现!!!我所做的一件事是自动布局,但我不确定这是否会影响键盘弹出。
回答by Methawin Intharakham
Swift 4 it worked for me
try it
Swift 4 对我
有用 试试吧
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.async {
self.textView.becomeFirstResponder()
}
}
回答by 4aRk Kn1gh7
I just tested it with a textfield by calling self.tf.becomeFirstResponder()
indside viewDidLoad()
function and its working absolutely fine. You'll need to toggle your keyboard by pressing command + Kas nflacco just pointed out and disable the hardware keyboard in the simulator as:
我只是通过调用self.tf.becomeFirstResponder()
indsideviewDidLoad()
函数使用文本字段对其进行了测试,并且它的工作非常好。正如 nflacco 刚刚指出的那样,您需要通过按command + K来切换键盘,并禁用模拟器中的硬件键盘,如下所示:
- iOS Simulator -> Hardware -> Keyboard
- Uncheck "Connect Hardware Keyboard"
- iOS 模拟器 -> 硬件 -> 键盘
- 取消选中“连接硬件键盘”
回答by Hoàng Kiên
I think
我认为
class BLTestViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var tf: UITextField
override func viewDidLoad() {
super.viewDidLoad()
tf.delegate = self
self.focustf()
// Do any additional setup after loading the view.
}
func focustf(){
self.tf.becomeFirstResponder()
}
}
回答by Derek Hierholzer
Swift 4 and iOS 11
斯威夫特 4 和 iOS 11
In my case, no mater what I tried, I was not able to set first responder until I tried this.
就我而言,无论我尝试过什么,在尝试此操作之前,我都无法设置第一响应者。
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if cell.isKind(of: YOURTABLEVIEWCELL.self) {
if let yourCell = cell as? YOURTABLEVIEWCELL{
yourCell.yourUiTextField.becomeFirstResponder()
}
}
}
Hope this helps someone stuck with the same problem.
希望这可以帮助遇到同样问题的人。
回答by Talha Ahmed
press command+shift+k for open the keyboard.
按 command+shift+k 打开键盘。
回答by x-rw
try with this
试试这个
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
textField.becomeFirstResponder()
}
sometimes the wrong is the time in render the view
有时错误的是渲染视图的时间