如何在编辑时更改returnKeyType?

Ali*_*mze 29 iphone objective-c uitextfield

如何在编辑时更改iPhone上的返回键.我知道

myTextField.returnKeyType = UIReturnKeySearch;
Run Code Online (Sandbox Code Playgroud)

但是,这只有在我打电话[myTextField resignFirstResponder];然后[myTextField becomeFirstResponder];隐藏键盘然后将其带回来时才有效.否则,它只是保持我以前的那个,在这种情况下它是"Go".我需要根据用户输入的字符继续切换它们.


在一行中:如何在键盘编辑时更改键盘的返回键类型?

Mar*_*ton 56

[textField reloadInputViews] 似乎有把戏......

  • reloadInputViews有一些警告.请参阅http://stackoverflow.com/questions/5958427/can-i-change-property-uireturnkeytype-of-uitextfield-dynamically.它还将键盘更改回其初始状态.因此,如果用户切换到其他键盘布局(数字,标点符号等)之一,则该状态将丢失. (8认同)

Ali*_*mze 6

我找了一会儿,忘了回到这里.我在用:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange: 
(NSRange)range replacementString:(NSString *)string {
    [self performSelector:@selector(addressBarTextDidChange:) withObject:nil afterDelay:0.1];
    return YES;
}

-(void)addressBarTextDidChange:(NSString *)text {
    NSString *addressText = @"..."; //Wherever you get your text from... 
    if ([addressText isEqualToString:@""]==NO) {
        if ([addressText rangeOfString:@" "].location != NSNotFound) {
            //[addressBarTextField setReturnKeyType:UIReturnKeySearch];
            if (addressBarTextField.returnKeyType!=UIReturnKeySearch) {
                [UIView beginAnimations:nil context:NULL]; //Setup the animation.
                [UIView setAnimationDuration:0.0]; //The duration for the animation.
                [addressBarTextField resignFirstResponder];
                addressBarTextField.returnKeyType = UIReturnKeySearch;
                [addressBarTextField becomeFirstResponder];
                [UIView commitAnimations];
            }
        } else {
            //[addressBarTextField setReturnKeyType:UIReturnKeyGo];
            if (addressBarTextField.returnKeyType!=UIReturnKeyGo) {
                [UIView beginAnimations:nil context:NULL]; //Setup the animation.
                [UIView setAnimationDuration:0.0]; //The duration for the animation.
                [addressBarTextField resignFirstResponder];
                addressBarTextField.returnKeyType = UIReturnKeyGo;
                [addressBarTextField becomeFirstResponder];
                [UIView commitAnimations];
            }
        }

    } else {
        if (addressBarTextField.returnKeyType!=UIReturnKeyDone) {
            [UIView beginAnimations:nil context:NULL]; //Setup the animation.
            [UIView setAnimationDuration:0.0]; //The duration for the animation.
            [addressBarTextField resignFirstResponder];
            addressBarTextField.returnKeyType = UIReturnKeyDone;
            [addressBarTextField becomeFirstResponder];
            [UIView commitAnimations];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

基本上,我正在为键盘输入和输出动画持续0.0秒(因此用户将看不到它).我也在检查键盘是否已经是切换之前我想要的键盘因为不断切换导致滞后.


Jav*_*ría 5

就我而言,它的工作原理如下:

sender.returnKeyType=UIReturnKeyNext;
[sender reloadInputViews];
[sender resignFirstResponder];
[sender becomeFirstResponder];
Run Code Online (Sandbox Code Playgroud)

我不得不强制使用risignFirstResponder,然后使用becomeFirstResponder.

  • 请注意,这存在密码字段(secureTextEntry)的缺陷。调用resignFirstResponder和beginFirstResponder会重置该字段,以使下一个键盘输入替换先前的文本(这类似于密码字段上的标准行为)。 (2认同)