将焦点转移到下一个Textfield

时间:2013-06-17 07:53:17

标签: 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,类似于其他修饰符文本字段。我在shouldChangeCharactersInRange:方法中做了这些限制。请在我的代码中建议处理此增强功能的方法。

2 个答案:

答案 0 :(得分:3)

@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;   
}

答案 1 :(得分:0)

if (textField==txt_modifier1)
{
 // 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;
    else
    [txt_modifier2 becomeFirstresponder];

    return NO;
   }
}
else if( textField==txt_modifier2 )
{
same code of above block just change the text filed on which you want to focus next
}
else if(textField==txt_modifier3 )
{
same code of above block just change the text filed on which you want to focus next
}
else if(textField==txt_modifier4) 
{
same code of above block just change the text filed on which you want to focus next
}

希望这会对你有所帮助。让我知道结果