1

I have a custom keyboard, which is set up with a UITextInput delegate for sending in text to the current text field. But I need to also send a 'return' press to invoke the textFieldShouldReturn method and as far as I can tell, UITextInput does not allow this. (unless there is some kind of specific character for the return key?)

So how exactly do you pass a 'return' key press value to a text field to trigger the textFieldShouldReturn?

JMD
  • 1,540
  • 2
  • 24
  • 56
  • See my answer here: http://stackoverflow.com/questions/13205160/how-do-i-retrieve-keystrokes-from-a-custom-keyboard-on-an-ios-app/13205494#13205494 – rmaddy Feb 21 '13 at 16:35

3 Answers3

2

The specific character for the return key is \n adding that to the end of your string will put your cursor on the next line...

*then to actually call the textFieldShouldReturn method, if you still want to do that for some reason, you just call it like you would any method.

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
  • I guess I mis spoke. I need this 'return' key press to dismiss the keyboard. Which means its a key press that invokes the textFieldShouldReturn method. Like you are going to dismiss the keyboard normally. – JMD Feb 21 '13 at 15:48
  • I'm confused... you have a custom keyboard and you want a return key on your custom keyboard which will hide the keyboard when it is pressed? – Albert Renshaw Feb 21 '13 at 15:49
  • Yes. I have a custom keyboard and when the user is finished inputting data, or they want to do something else instead, I have a 'done' button they can press to dismiss the keyboard. And the question is, basically, how do I accomplish this since I currently only have a UITextInput delegate set up. – JMD Feb 21 '13 at 15:51
  • So you're saying a UITextField is selected, but your custom keyboard shows instead of the built in one? So when your custom keyboard is "returned" just unselect the current UITextField and then use this method instead `- (void)textFieldDidEndEditing:(UITextField *)textField` – Albert Renshaw Feb 21 '13 at 15:58
  • Also, what exactly are you trying to accomplish by calling `textFieldShouldReturn`? Are you trying to hide your custom keyboard, because there are much better ways of doing that :) – Albert Renshaw Feb 21 '13 at 16:00
  • First, how do I trigger my custom keyboard being 'returned' ? Second, I can't dismiss the keyboard in the textFieldDidEndEditing method because the user needs to have the ability to switch between text fields with arrow keys and that would dismiss the keyboard every time they use an arrow to move to a new field. – JMD Feb 21 '13 at 16:01
  • Yes, if you know a better way to hide my custom keyboard then please explain. – JMD Feb 21 '13 at 16:01
  • Okay, I see... you are confused.... Apple's built in keyboard methods like `textFieldShouldReturn` only apply to their built in keyboard, if you made your own keyboard then you need to manually hide it... either animate it off of the screen or just say something like `myKeyboardView.hidden = YES;` when your custom return key is pressed. – Albert Renshaw Feb 21 '13 at 17:01
1

You should implement this:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}
apascual
  • 2,970
  • 1
  • 20
  • 32
0

You're headed in the wrong direction here, because you're already using a custom keyboard. That should eliminate you from needing to use a UITextFieldDelegate to solve the problem of return key detection.

textFieldShouldReturn: is a delegate method of the UITextField used to detect when the user presses the return key. If you didn't have this method, you'd have to use textField:shouldChangeCharactersInRange:replacementString: to detect the newline character from a normal UIKeyboard, which would be a pain in the butt.

But if you have a particular button on your keyboard that should do something special, just wire that button up to your IBAction method directly.

So something like this:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
   [self returnKeyPressed:textField];
   return NO;
}

- (IBAction)returnKeyPressed:(id)sender {
   // Do whatever you want done
}

If you wire your custom key to returnKeyPressed:, both a hardware keyboard and your virtual custom keyboard would end up in returnKeyPressed: and the behavior would be consistent.

You probably would want to define a small protocol to make sure your delegates support returnKeyPressed: in addition to the UITextFieldDelegate methods.

AlleyGator
  • 1,266
  • 9
  • 13
  • Where do i implement the returnKeyPressed Method? The view with the text field? or the view of the keyboard? I assume the view of the keyboard and then delegate it in the view with the text field? – JMD Feb 21 '13 at 16:19
  • @AlleyGator It's not the wrong direction. If done properly, the text field should work the same whether a regular keyboard or the custom keyboard is setup. This means the view controller implements the usual text field delegate methods. The view controller should not need to know that a custom keyboard is being used. Of course this means that the custom keyboard must be implemented properly so the proper delegate methods are called as keys are tapped. – rmaddy Feb 21 '13 at 16:38
  • @JesseDurham If it can always do the same thing as a normal return key and doesn't need special behavior, you can easily do the second option you mention. Implement the method in the view of the keyboard and delegate to the view with the text field. I'm having to guess about your design a bit, but for simple situations that should be OK. – AlleyGator Feb 21 '13 at 22:18
  • @rmaddy I suppose. I didn't think that would be a given, because then he might have to respond to the BOOL value of textFieldShouldReturn: and manually insert newlines into the textField based on what the delegate is telling him. If he doesn't ever have to worry about that, then that's a nice simplification. – AlleyGator Feb 21 '13 at 22:25
  • @AlleyGator But newlines are never inserted into a text field regardless of what `textFieldShouldReturn:` returns. – rmaddy Feb 21 '13 at 22:42
  • You are correct, of course... I was thinking of text views, not text fields. – AlleyGator Feb 22 '13 at 20:05