How to open the keyboard automatically on UITextField?

IphoneObjective CInterfaceUitextfieldBuilder

Iphone Problem Overview


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.

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 ?

Thanks

Iphone Solutions


Solution 1 - Iphone

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.

Solution 2 - Iphone

Swift 3 & 4:

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

Solution 3 - Iphone

override func viewDidLoad() {
    super.viewDidLoad()
    textField.becomeFirstResponder()
}

Solution 4 - Iphone

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioneemceebeeView Question on Stackoverflow
Solution 1 - IphonejessecurryView Answer on Stackoverflow
Solution 2 - IphoneNico S.View Answer on Stackoverflow
Solution 3 - IphoneShubham GoelView Answer on Stackoverflow
Solution 4 - IphoneYashView Answer on Stackoverflow