1

If a user is entering a phone number into a UITextField the phone keypad has no return key (unlike the text entry keyboard) - therefore when/how does -textFieldShouldReturn: get called in order to get the number and dismiss the keyboard?

Simon Goldeen
  • 9,080
  • 3
  • 36
  • 45
Gruntcakes
  • 37,738
  • 44
  • 184
  • 378

3 Answers3

0

-textFieldShouldReturn: is not the only delegate method available to you. The method - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string gets called on you each time the user enters or deletes a character. You could respond to the user entering the last character at that time if you wish.

Alternately, you could have a button near the text field that the user could touch when they are done and have it function just like a return key.

Simon Goldeen
  • 9,080
  • 3
  • 36
  • 45
0

The recommended way to add a "done", "return", etc. button to the numeric keypad is to add a keyboard accessory view. You can add a button to the accessory view then program that button to do whatever you want.

Check out this tutorial: Customizing the iOS keyboard

Alternatively, you can monitor the text in the field and act when you have enough data. For example, when you enter your PIN on the lock screen, you don't tap "Return". It automatically attempts to unlock the screen once you've entered in 4 digits.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
-1

Following code should help [will be called when user taps outside of textfield]

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

Don't forget to implement <UITextFieldDelegate> and also set textField.delegate = self in your view controller.

Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
  • That method doesn't get called if the user taps anywhere outside of the text field (yes I have my delegate set up correctly, I receive a textFielDidBeginEditing call but never a textFieldSHouldEndEditing). – Gruntcakes Jul 12 '13 at 18:04
  • If -textFieldShouldReturn: gets called use it - your objective is to anyhow call resignFirstResponder. – Nirav Bhatt Jul 12 '13 at 18:14
  • As I said in my question, and which is the whole point of my question: "therefore when/how does -textFieldShouldReturn: get called". The whole point of my question is that textFieldShouldReturn: does not get called because there is no return key on the phone number entry keypad. – Gruntcakes Jul 12 '13 at 18:30
  • Does this help? http://stackoverflow.com/questions/788323/disable-enable-return-key-in-uitextfield – Nirav Bhatt Jul 12 '13 at 18:44
  • There isn't a return key on the phone keypad. And there's no point in assigning that functionality to another key if that's possible because then the keypad would dismiss when the user intended to enter or erase a character. – Gruntcakes Jul 12 '13 at 20:15