Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a UITextField into editing mode programmatically

I have a UITextField that I want to set into editing mode (keyboard on screen and cursor in text field box) programatically. I know that the user will be in editing mode when this view appears onscreen, so I want to save the user from having to tap the text field.

The "editing" property of a UITextField is read only - so that doesn't work. Is there a way to set the UITextField into editing mode, with a keyboard onscreen, programmatically?

like image 672
Don Wilson Avatar asked Nov 09 '10 05:11

Don Wilson


3 Answers

Call becomeFirstResponder on the UITextField.

Related question: How do I show the keyboard by default in UITextView?

like image 130
Tyler Avatar answered Nov 15 '22 23:11

Tyler


You have to call [textField becomeFirstResponder];

like image 28
Thomas Huber Avatar answered Nov 15 '22 23:11

Thomas Huber


Indeed call [textField becomeFirstResponder] in Obj-C or textField.becomeFirstResponder() in Swift.

However, make sure you call this in the viewDidAppear and not in the viewDidLoad to prevent strange behaviour (see: When set UITextField as FirstResponder programmatically, cause some weird actions on text editing).

like image 12
Tom Avatar answered Nov 16 '22 00:11

Tom