Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set focus to next uitextfield in iPhone?

I have 8 uitextfields that are for a password. Each textfield contains one letter from password. When I type one letter and after that when I press the 2nd key then focus moves to next textfield. But I want that, as soon as I finished typing first character, the focus should move to the next text field without pressing other key or pressing tab key. How can I do that? Any tutorial of sample code for that? The code I have used is as below.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(keyPressed:) name: UITextFieldTextDidChangeNotification object: nil];
    // Try to find next responder

    UIResponder* nextResponder = [textField.superview viewWithTag:nextTag];
    if (nextResponder) {
        // Found next responder, so set it.
        //nextTag += 1;
        [nextResponder becomeFirstResponder];
        if ([nextResponder tag] == 308) {
            //[self performSelector:@selector(checkPassCode) withObject:nil afterDelay:0.0];
            [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(checkPassCode) userInfo:nil repeats:NO];
            lastResponder = nextResponder;
        }
        return YES;
    } else {
        [textField resignFirstResponder];
        return NO;
    }
}


-(void) keyPressed: (NSNotification*) notification
{
    nextTag += 1;
    [[NSNotificationCenter defaultCenter] removeObserver: self name:UITextFieldTextDidChangeNotification object: nil];
}
like image 259
nehal Avatar asked May 23 '11 12:05

nehal


3 Answers

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString *finalString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if ( [finalString length] > 0 ) {
        textField.text = string;

        UIResponder* nextResponder = [textField.superview viewWithTag:(textField.tag + 1)];
        if (nextResponder) {
            [nextResponder becomeFirstResponder];

            // Your code for checking it.
        }

        return NO;
    }

    return YES;
}

Hope this helps.

Swift 3.2

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let finalString: String? = (textField.text? as NSString).replacingCharacters(in: range, with: string)
    if (finalString?.characters.count ?? 0) > 0 {
        textField.text = string
        let nextResponder: UIResponder? = textField.superview?.viewWithTag((textField.tag + 1))
        if nextResponder != nil {
            nextResponder?.becomeFirstResponder()
            // Your code for checking it.
        }
        return false
    }
    return true
}

Swift 4.1

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    let finalString: String? = (textField.text as NSString?)?.replacingCharacters(in: range, with: string)
    if finalString?.count > 0 {
        textField.text = string
        let nextResponder: UIResponder? = textField.superview?.viewWithTag(textField.tag + 1)
        if nextResponder != nil {
            nextResponder?.becomeFirstResponder()
            // Your code for checking it.
        }
        return false
    }
    return true
}
like image 53
Deepak Danduprolu Avatar answered Oct 23 '22 13:10

Deepak Danduprolu


-(BOOL)textFieldShouldReturn:(UITextField *)textField {

    if (textField ==firstTextField) {
        [secondTextField becomeFirstResponder];
    }else if ( textField == secondTextField){
        [thirdTextField becomeFirstResponder];
    }else if ( textField == thirdTextField){
        [fourthTextField becomeFirstResponder];
    }else{
        [textField resignFirstResponder];
    }
        return YES;
}
like image 43
Kirit Vaghela Avatar answered Oct 23 '22 14:10

Kirit Vaghela


Try this add notification in ViewDidLoad,

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(limitTextField:) name:@"UITextFieldTextDidChangeNotification" object:txtField1];

- (void)limitTextField:(NSNotification *)note {
    if(txtField1.tag==1)
    {
        if ([[txtField1 text] length] > 1)
        {
           [txtField1 resignFirstResponder];
           [txtField2 becomeFirstResponder];
        }
    }
}
like image 28
nambi Avatar answered Oct 23 '22 14:10

nambi