ios - 将焦点转移到下一个文本字段

标签 ios

我有一组 4 个文本字段。我对这些文本字段应用了某些限制,可以通过下面的代码轻松理解。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString * newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if (string.length==0) {
        return YES;
    }


    if (textField == txt_mins) {
        return [[AppDelegate sharedInstance] setNumberFormat:textField newString:newString
        IntegerLimit:5 FractionLimit:2 NumberType:2];
    }
    else if (textField == txt_units){
        return [[AppDelegate sharedInstance] setNumberFormat:textField newString:newString 
        IntegerLimit:5 FractionLimit:2 NumberType:2];
    }
    // Below logic is for All 4 Modifer Textfields
    // we are restricting the user to enter only max 2 characters in modifier textfields and
     also automatically
    // converting each entered character to the uppercase string.
    if (textField==txt_modifier1 || textField==txt_modifier2 || textField==txt_modifier3 ||
     textField==txt_modifier4) {
        // This condition is restricting the user from entering the special characters in the
        modifier textfield.
        if ([string rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] 
        invertedSet]].location != NSNotFound) {
            // There are non-alphanumeric characters in the replacement string
            return NO;
        }
        else{
        NSString *result = [textField.text stringByReplacingCharactersInRange:range 
        withString:string.uppercaseString];
        if (result.length <= 2)
            textField.text = result;
        return NO;
       }
    }
    return YES;
}

我真正想要的是 txt_modifier1。一旦我在文本字段中键入我的两个字符,焦点就应该转移到下一个文本字段 txt_modifier2 ,对于其他修饰 rune 本字段也是如此。我在 shouldChangeCharactersInRange: 方法中做了这些限制。请建议在我的代码中处理此增强功能的方法。

最佳答案

@property UIView *firstResponder;

- (void) toggleTextfield 
{
    NSInteger nextTag = self.firstResponder.tag;
    nextTag += 1;
    UITextField *nextTextField = (UITextField *)[self.view viewWithTag:nextTag];  
    if (nextTextField)  
    {  
        [nextTextField becomeFirstResponder];  
    }  
}  

将标记值分配给您想要使用的所有文本字段。
当你想转移焦点时调用这个方法。

textFieldDidBeginEditing: 中添加这一行

self.firstResponder = textField;

Replace this code 
if (result.length <= 2)
  textField.text = result  
return NO; 

with 

 if (result.length <= 2) 
        textField.text = result; 
    else [self toggleTextfield]; 

and add this function - 

(void)textFieldDidBeginEditienter code hereng:(UITextField *)textField   
{  
     self.firstResponder = textField;   
}

关于ios - 将焦点转移到下一个文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17142749/

相关文章:

ios - 尝试执行 UIAlert 或 PerformSegueWithIdentifier 时,以 NSException 类型的未捕获异常终止

ios - Apple Watch,添加时将复杂功能注册到后端服务器

ios - UIPrintPageRenderer paperRect 始终返回 0x0

ios - 如何正确子类化 UIActionSheet

ios - 委托(delegate)文本字段并将其限制为仅输入数字

ios - 从我的应用程序向 iOS 语音控制引擎注册新的语音命令

ios - 如何在 Swift 中传入一个 nil 通知变量

ios - CollectionViewCell 中的 UILabel 不阻止触摸事件

ios - “order”(bootstrap 4 网格)在 Safari IOS 中无法正常工作

ios - 对成员 'Int.init' 的模糊引用