UITextFields: event click to "Return key" on keyboard
The keyboard includes a “return key”, which can be linked to any action, such as moving to the next field on the form, this we can do with overload function textFieldShouldReturn.
Often we have a group of text fields and want to be able to switch from one to the next using the keyboard.
1. Create interface in h file
Inteface define properties and must contain UITextFieldDelegate as delegate class.
Objective-C
1 2 3 4 5 6 |
@interface LoginSCR : TableView <loginManagerDelegate, UITextFieldDelegate> @property (nonatomic, retain) IBOutlet UITextField *username; @property (nonatomic, retain) IBOutlet UITextField *userpassword; @end |
Set delegate in Xib file
When we created UITextFields in the header file, you can connect with XIB file.
What is important: you need to set element delegate to File’s Owner.
Overload function textFieldShouldReturn
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@implementation LoginSCR @synthesize username; @synthesize userpassword; .... // called when "Next" is pressed - (BOOL) textFieldShouldReturn:(UITextField *)textField { //if filed is username if (textField == username) { //set responder to next element [userpassword becomeFirstResponder]; //if field is userpassword } else if (textField == userpassword) { //for example: submit form [self loginAction:self]; } return YES; } |
Setting options of “Return key”
Posted on 10 April 2013