How do I give a UITextView focus programmatically?

前端 未结 6 1527
[愿得一人]
[愿得一人] 2020-12-25 09:19

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

6条回答
  •  [愿得一人]
    2020-12-25 09:58

    That does seem somewhat hackish.

    The Cocoa Touch terminology for 'having focus' is 'first responder' and UITextViews will display their keyboards when they are the first responder. I can think of several techniques to make the UITextView become the first responder, but the easiest is probably in your view controller's viewWillAppear or viewDidAppear methods:

    - (void)viewWillAppear:(BOOL)animated
    {
    
        [myTextView becomeFirstResponder];
    
        [super viewWillAppear:animated];
    }
    

提交回复
热议问题