开发者

Implementing the 'next' option in the keyboard for iPhone SDK

开发者 https://www.devze.com 2022-12-15 02:49 出处:网络
I am having an awful hard time trying to implement the task of when the user selects the \'next\' button in the keyboard, the user is then sent to the next text field to start editing. In my example I

I am having an awful hard time trying to implement the task of when the user selects the 'next' button in the keyboard, the user is then sent to the next text field to start editing. In my example I have three text fields. Here is what I've done, hopefully you can fill me in on where I am going wrong. Keep in mind I just picked up SDK a few weeks ago so that may be part of the problem :)

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

-(IBAction)nextPressed:(id) sender;

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

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

In my .xib file, I have linked the first text field (right clicking on the text field and dragging to Files Owner and selecting the 'nextPres开发者_如何转开发sed:' option under Events) to my File Owner. I have tried linking to just the first text field and when that didn't work all of the three text fields. I've also tried linking not to the File's Owner but First Responder for one text field, then all of the text fields, with no luck. Also, for each text field I have selected the Return Key as 'NEXT'.

Now when I Build/Run I am able to edit the text field and see the 'next' button in the lower right, however it doesn't move me to the next field.

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

Any help with this would be greatly appreciated. I've been staring at this for the past four hours and Googling every possible search term I can come up with and can't easily wrap my head around the steps needed to accomplish this. Again, and help would be very helpful :)


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

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

That particular method watches for when the Enter key (in your case, a Next key) is pressed. Once that's implemented, just set the delegate for each of the three text fields to the implementing class (probably your view controller), and you should be good.


You can use following code......

Assign returnKeyType property

@property(nonatomic) UIReturnKeyType returnKeyType;  

UITextField *myTextField; // your textfield..
myTextField.returnKeyType = UIReturnKeyNext;

You can assign following types to a return key of virtual keyboar

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


You should set each of the text fields delegate to your view controller. Then, in your view controller, implement the UITextFieldDelegate. Finally, simply add the following function to your view controller .m file:

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

I think where you are going wrong is thinking of it as an event you connect to. Instead, you need to implement this delegate somewhere in your code and tell your UITextField which class is the delegate for it.


Tim above is on the ball. This 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

精彩评论

暂无评论...
验证码 换一张
取 消