如何让键盘自动弹出

时间:2022-03-01 00:16:15

I would like the keyboard to be shown without the user touch the username text field.

我想在没有用户触摸用户名文本字段的情况下显示键盘。

I found the code from How to open the keyboard automatically on UITextField?

我找到了如何在UITextField上自动打开键盘的代码?

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

So I tried this

所以我尝试了这个

@IBOutlet var userNameTF: UITextField!

@IBOutlet var passwordTF: UITextField!


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

    userNameTF.becomeFirstResponder()

}

but it gives an error

但它给出了一个错误

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

线程1:致命错误:在展开Optional值时意外发现nil

2 个解决方案

#1


0  

Double check your outlets are connected if you are using the storyboard, and then try this:

如果您使用故事板,请仔细检查您的插座是否已连接,然后尝试以下操作:

1) Place your userNameTF.becomeFirstResponder in viewDidLoad() instead of viewDidAppear().

1)将您的userNameTF.becomeFirstResponder放在viewDidLoad()而不是viewDidAppear()中。

2) Convert

@IBOutlet var userNameTF: UITextField!
@IBOutlet var passwordTF: UITextField!

to:

@IBOutlet weak var userNameTF: UITextField!
@IBOutlet weak var passwordTF: UITextField!

#2


0  

I think problem is with IBOutlets

我认为问题出在IBOutlets上

@IBOutlet var userNameTF: UITextField!

1) You are using a strong reference here which will cause you issue later that even after the presented View with these TF is removed memory will not be released as reference to property are strong

1)你在这里使用强引用,这将导致你稍后问题,即使在呈现的视图与这些TF被删除后,内存将不会被释放,因为对属性的引用很强

2) Try using @IBOutlet weak var usernameTF: UITextField! and when you make changes remove the connected outlet from storyBoard and reconnect it with Written outlet and build

2)尝试使用@IBOutlet weak var usernameTF:UITextField!当您进行更改时,从storyBoard中删除已连接的插座并将其与Written outlet重新连接并构建

Crash - Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

崩溃 - 线程1:致命错误:在展开Optional值时意外发现nil

occurs in two case -

发生在两种情况下 -

1) This is occurring as you are trying to access a property which is not properly connected 

2) When you are Presenting the same presented view In hierarchy 

Try using Solutions mentioned and let me know if issue still exists

尝试使用提到的解决方案,如果问题仍然存在,请告诉我

import UIKit

class TFVC: UIViewController,UITextFieldDelegate {

    @IBOutlet weak var usernameTF: UITextField!
    @IBOutlet weak var passowrdTF: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        usernameTF.delegate = self
        passowrdTF.delegate = self

    }

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

#1


0  

Double check your outlets are connected if you are using the storyboard, and then try this:

如果您使用故事板,请仔细检查您的插座是否已连接,然后尝试以下操作:

1) Place your userNameTF.becomeFirstResponder in viewDidLoad() instead of viewDidAppear().

1)将您的userNameTF.becomeFirstResponder放在viewDidLoad()而不是viewDidAppear()中。

2) Convert

@IBOutlet var userNameTF: UITextField!
@IBOutlet var passwordTF: UITextField!

to:

@IBOutlet weak var userNameTF: UITextField!
@IBOutlet weak var passwordTF: UITextField!

#2


0  

I think problem is with IBOutlets

我认为问题出在IBOutlets上

@IBOutlet var userNameTF: UITextField!

1) You are using a strong reference here which will cause you issue later that even after the presented View with these TF is removed memory will not be released as reference to property are strong

1)你在这里使用强引用,这将导致你稍后问题,即使在呈现的视图与这些TF被删除后,内存将不会被释放,因为对属性的引用很强

2) Try using @IBOutlet weak var usernameTF: UITextField! and when you make changes remove the connected outlet from storyBoard and reconnect it with Written outlet and build

2)尝试使用@IBOutlet weak var usernameTF:UITextField!当您进行更改时,从storyBoard中删除已连接的插座并将其与Written outlet重新连接并构建

Crash - Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

崩溃 - 线程1:致命错误:在展开Optional值时意外发现nil

occurs in two case -

发生在两种情况下 -

1) This is occurring as you are trying to access a property which is not properly connected 

2) When you are Presenting the same presented view In hierarchy 

Try using Solutions mentioned and let me know if issue still exists

尝试使用提到的解决方案,如果问题仍然存在,请告诉我

import UIKit

class TFVC: UIViewController,UITextFieldDelegate {

    @IBOutlet weak var usernameTF: UITextField!
    @IBOutlet weak var passowrdTF: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        usernameTF.delegate = self
        passowrdTF.delegate = self

    }

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