Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I give a UITextView focus programmatically?

So I want to bring in a modal view controller that has a UITextView in it and I want the keyboard to automatically popup and the UITextView have focus.

I found a way to accomplish this by doing the following:

textView.editable = YES; textView.editable = NO; 

This just seems hacky to me, is there another way?

like image 990
Meroon Avatar asked Jun 12 '09 17:06

Meroon


2 Answers

Since UITextView inherits from UIResponder (indirectly, it actually inherits from UIScrollView, which inherits from UIView which then inherits from UIResponder) you can call the -becomeFirstResponder method on your text field, which will cause it to become the first responder and begin editing:

[textView becomeFirstResponder]; 
like image 55
Alex Rozanski Avatar answered Oct 09 '22 22:10

Alex Rozanski


Swift:

textView.becomeFirstResponder() 
like image 37
Esqarrouth Avatar answered Oct 09 '22 23:10

Esqarrouth