dynamically change UIKeyboards return key

2020-02-12 07:21发布

问题:

I have two UITextfield the user enters his name into the first and email into the second. I would like to know how to change the UIKeyboards return key depending if the name text field has an entry or not.

For instance if nametextfield is empty then I would like the UIkeyboard return key to be Next else if the nametextfield has an entry in it then when the user selects the email text field I would like the return key to be submit.

Is this possible? if so how would I go about accomplishing it? any help would be appreciated.

回答1:

You can have return key customized to prefixed values that you can see in UIReturnKeyType enum for each UITextField.

textFieldName.returnKeyType = UIReturnKeyNext;
textFieldEmail.returnKeyType = UIReturnKeyDefault;

Not sure if this is what you're looking for though.



回答2:

You have a chance to set up keyboard characteristics in the UITextFieldDelegate Protocol method textFieldShouldBeginEditing: which is called before the text field becomes the first responder (indeed to decide if it may become the first responder). If you don't already have a delegate for the text field(s) in question you would have to assign one and implement at least that method. Presumably the same object handling the text field could hold the delegate methods. The following implementation sets the return key to "Search".

- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
    NSLog(@"textFieldShouldBeginEditing");
    textField.returnKeyType = UIReturnKeySearch;
    return YES;
}

You'd have to look at the contents of your text fields to decide what value to use.



回答3:

Make use of the textField.returnKeyType property.

you can check out all the available options here http://developer.apple.com/library/ios/documentation/uikit/reference/UITextInputTraits_Protocol/Reference/UITextInputTraits.html#//apple_ref/doc/c_ref/UIReturnKeyType



回答4:

textfield.returnKeyType = UIReturnKeySearch;