- Adding a decimal point to the numeric keyboard
[permalink]
Apple added a decimal pad keyboard type. As of Xcode 4.1, not exported in the UI, so you have to set it in code:
textField.keyboardType = UIKeyboardTypeDecimalPad;
- Bringing up the phone keyboard
[permalink]
[self.nameField becomeFirstResponder];
If you want the keyboard to animate in after your view appears, you can call this in your -viewWillAppear: - Dismissing the on-screen keyboard
[permalink]
If you know the text field that is being edited, you can tell it to resign first responder:
[textField resignFirstResponder];
Otherwise, you can tell the enclosing view to end editing, and it'll figure out who is editing and tell them to resign:
[self.view endEditing: YES];
- Getting notified of UITextField changes
[permalink]
Easiest way is to hook up the Editing Changed (NOT Value Changed) action. - Handling keyboard return button
[permalink]
Notifications about the UITextField keyboard return button come through this delegate method. This one hides the keyboard when the return button is typed.
- (BOOL) textFieldShouldReturn: (UITextField *) textField {
// Dismiss the keyboard.
[self.view endEditing:YES];
return YES;
}
- Only allowing numbers in a text field
[permalink]
Sometimes you need a more general keyboard for a numeric-only text field. You can do something like this in a delegate method:
- (BOOL) textField: (UITextField *) textField
shouldChangeCharactersInRange: (NSRange) range
replacementString: (NSString *) string {
NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range
withString: string];
if (resultingString.length == 0) return YES;
NSScanner *scanner = [NSScanner scannerWithString: resultingString];
float throwaway;
BOOL scansFloat = [scanner scanFloat: &throwaway];
BOOL atEnd = [scanner isAtEnd];
return scansFloat && atEnd;
} // shouldChangedCharacersInRanges
(thanks to Frank Shearer, from a SO post)