Implementing the “next” keyboard option for the iPhone SDK

I have a terrible time trying to implement the task, when the user selects the "next" button on the keyboard, the user then goes to the next text field to start editing. In my example, I have three text fields. This is what I have done, I hope you can fill me with where I am wrong. Keep in mind that I just took the SDK a few weeks ago so this could be part of the problem :)

In my ViewControler.h file, I created the following method

-(IBAction)nextPressed:(id) sender; 

In my ViewController.m file, I created the following action

 -(IBAction)nextPressed:(id) sender { if ([txtUserName isFirstResponder]) { [txtUserName2 becomeFirstResponder]; } if ([txtUserName2 isFirstResponder]) { [txtUserName3 becomeFirstResponder]; } } 

In my .xib file, I linked the first text box (by right-clicking on the text box and dragging it to the “File Owner” and selecting “nextPressed: Events” in the Events section) to my file owner. I tried to associate only with the first text field, and when it did not work in all three text fields. I also tried to associate not with the file owner, but with the first responder for one text field, and then with all the text fields, with no luck. In addition, for each text field, I selected Return Key as "NEXT".

Now, when I create / run, I can edit the text field and see the “next” button in the lower right corner, however it does not transfer me to the next field.

What step am I doing wrong here? I used the instructions from this post ( How do you change UIControl with a focus on the iPhone? ), But there seems to be something huge missing.

Any help with this would be greatly appreciated. I have looked at this for the past four hours, and Googling I can come up with all the possible search conditions and cannot easily wrap my head around the steps necessary to achieve this. Again, and help will be very helpful :)

+4
source share
4 answers

I don't think you need to define a separate nextPressed: selector nextPressed: - instead, implement the protocol method UITextFieldDelegate textFieldShouldReturn: to see something like:

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { if(textField == txtUserName) { [txtUserName2 becomeFirstResponder]; } else if(textField == txtUserName2) { [txtUserName3 becomeFirstResponder]; } return NO; } 

This particular method keeps track of when the Enter key is pressed (in your case, the Next key). Once this is implemented, just set the delegate for each of the three text fields to the implementation class (maybe your view controller), and you should be good.

+22
source

You can use the following code ......

Assign property returnKeyType

 @property(nonatomic) UIReturnKeyType returnKeyType; UITextField *myTextField; // your textfield.. myTextField.returnKeyType = UIReturnKeyNext; 

You can assign the following types to the virtual keychain return key

 typedef enum { UIReturnKeyDefault, UIReturnKeyGo, UIReturnKeyGoogle, UIReturnKeyJoin, UIReturnKeyNext, UIReturnKeyRoute, UIReturnKeySearch, UIReturnKeySend, UIReturnKeyYahoo, UIReturnKeyDone, UIReturnKeyEmergencyCall, } UIReturnKeyType; 
+2
source

You must set each delegate text field to your view controller. Then in your view controller, execute UITextFieldDelegate . Finally, just add the following function to the .m file of the controller file.

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { if (textField == txtUsername) [txtUsername2 becomeFirstResponder]; else if (textField == txtUsername2) [txtUsername3 becomeFirstResponder]; } 

I think that you are mistaken in thinking of it as an event to which you are connecting. Instead, you need to implement this delegate somewhere in your code and tell your UITextField which class is its delegate.

+1
source

Tim is higher on the ball. It worked for me too! I implemented the following with multiple keyboards.

in your ViewController.m:

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { if(textField == txtCoffeeName) { [txtCompanyName becomeFirstResponder]; } else if(textField == txtCompanyName) { [txtPrice becomeFirstResponder]; } return NO; } 
0
source

All Articles