Swift:成为 UITextField 上的第一响应者不工作? [英] Swift: Become First Responder on UITextField Not Working?

查看:41
本文介绍了Swift:成为 UITextField 上的第一响应者不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Storyboard 上创建了一个带有一个 UITextField 的自定义 UIViewController.在 viewDidLoad 上,我将 UITextFIeld 设置为 becomeFirstResponder,什么也没发生(没有键盘弹出).

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).

然后我尝试调用 resignFirstResponder(),但它返回 false.接下来,我试图通过使用@在不使用私有 API 的情况下获取当前的第一响应者.事实证明,没有一个子视图是第一响应者.

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.

故事板中的我的 ViewController

My ViewController in storyboard

我的代码

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.

推荐答案

Swift 4 对我有用
试试吧

Swift 4 it worked for me
try it

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    
    textView.becomeFirstResponder()
    
}

这篇关于Swift:成为 UITextField 上的第一响应者不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆