17

I got a TextView that is supposed to show upon a button press, and let the user enter text through the keyboard.

- (IBAction) changeName {

    changeNameBtn.hidden = YES;
    backBtn.hidden = YES;

    usernameLabel.hidden = NO;
    inputTextField.hidden = NO;
    [inputTextField becomeFirstResponder]; // to show keyboard
}

This works fine for most of us.

But the Keyboard does not show for some of my testers, at a geographically remote location. How to trouble shoot this? And what could be the reason for the different behavior of our devices? It works fine for me and everybody else. We all use the same devices (iPod, iOS 4.1 - 5.1).

First thing I tried did not work:

I guessed that the inputTextField did in some way not become visible in time, before becomeFirstResponder was fired, so I added setNeedsDisplay as I thought that would force an update.

[inputTextField setNeedsDisplay];
[inputTextField becomeFirstResponder];

Second thing I tried did not work:

if([inputTextField canBecomeFirstResponder]){
    [inputTextField becomeFirstResponder];
}

Third thing I tried is a workaround, requiring the unlucky user to press the field:

- (IBAction) inputTextFieldTouch {
    [inputTextField becomeFirstResponder];
}

This workaround works, but there must be a better way. Please enlighten me about what I am doing wrong! :) And how to approach issues like this without being able to duplicate it myself?

JOG
  • 5,590
  • 7
  • 34
  • 54

4 Answers4

27

I called becomeFirstResponder later by this line:

[self.searchBar performSelector:@selector(becomeFirstResponder) 
                     withObject:nil 
                     afterDelay:0.1f];

It worked for me. Hope this help.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
jeswang
  • 1,017
  • 14
  • 26
10

The textview has not finished being drawn by the time you try to make it the first responder.

You can try animating the appearance and setting the first responder in the completion block.

[UIView animateWithDuration:0.0 animations:^{
   [view addSubview:textView];
} completion:^(BOOL finished){
   [textView becomeFirstResponder];
}];
Wombat
  • 316
  • 3
  • 5
5

As of iOS8, calling becomeFirstResponder on a UITextView doesn't show the keyboard for me on the iOS simulator, but it does on the real device. So be sure to check it with an actual device. This doesn't happen on iOS7 though.

SarpErdag
  • 781
  • 1
  • 8
  • 19
  • 11
    Have you pressed Cmd + Shift + K to use your computer's keyboard? That shows and hides the Simulator's keyboard for me. – chedabob Nov 24 '14 at 10:28
  • All my testers use actual devices, including the ones where the keyboard did not show. As the question states, "We all use the same devices (iPod, iOS 4.1 - 5.1)". – JOG Jan 01 '15 at 01:39
  • Thank you for saving me a lot of time trying to figure this out :D – Aviram Netanel Mar 05 '19 at 14:32
0

Try to see if you can show the keyboard first:

if([inputTextField canBecomeFirstResponder]){
    [inputTextField becomeFirstResponder];
}
Rui Peres
  • 25,741
  • 9
  • 87
  • 137