39

I want to create a view that consists solely of a UITextView. When the view is first shown, by default, I'd like the keyboard to be visible and ready for text entry. This way, the user does not have to touch the UITextView first in order to begin editing.

Is this possible? I see the class has a notification called UITextViewTextDidBeginEditingNotification but I'm not sure how to send that, or if that is even the right approach.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
bpapa
  • 21,409
  • 25
  • 99
  • 147

3 Answers3

69

to accomplish that just send the becomeFirstResponder message to your UITextField, as follows (assuming you have an outlet called textField, pointing to the field in question):

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [textField becomeFirstResponder];
}
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Adam Alexander
  • 15,132
  • 5
  • 42
  • 41
20

In Swift

To automatically show the keyboard, to the following:

override func viewDidLoad() {
    super.viewDidLoad()
    
    // show keyboard
    textView.becomeFirstResponder()
}

Notes

  • This assumes that the text view is editable.
  • Works for both UITextView and UITextField
  • To hide the keyboard use textView.resignFirstResponder()
Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
6

Following worked fine for me using Swift

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    // Show keyboard by default
    billField.becomeFirstResponder()
}

Key is to use the viewDidAppear function.

Yoichi Tagaya
  • 4,547
  • 2
  • 27
  • 38
Uthman
  • 9,251
  • 18
  • 74
  • 104