Search code examples
iphoneiosios6

How can you program a UITextField's keyboard to open on viewDidLoad?


I want my UITextField's keyboard to stay open for the entire time I use this view controller. I don't want it to only open when my user touches the text field. In order to do this, I was hoping I would call the textFieldShouldBeginEditing method by doing this:

EDIT: thanks everyone, I just noticed I called my UITextField a UIImage field for some reason in the interface.  

Solution

  • The textFieldShouldBeginEditing delegate method is not something that you call from your code. The OS calls the method when that particular event occurs, and you put code in there to run when the event is fired (similar to putting code in viewDidLoad for your view controller).

    To show the keyboard whenever the view controller appears, simply call the UITextField's becomeFirstResponder method in the view controller's viewDidAppear method like this:

    [self.myTextField becomeFirstResponder];
    

    Don't forget to create an IBOutlet parameter for the UITextField, link it in Interface Builder, and replace self.myTextField above with the outlet that you created.