Navigate through next button in textFields iOS swift

Problem:

I want to navigate to next field that wants to be focusable when i click next button.If i click the end textfield then keyboard wants to be hidden.


Solution:

In textfield delegate method do the following.

Objective c:


    -(BOOL)textFieldShouldReturn:(UITextField*)textField
    {
    NSInteger nextTF = textField.tag + 1;
    UIResponder* nextResponder = [textField.superview viewWithTag:nextTF];
    if (nextResponder) {
    [nextResponder becomeFirstResponder];
    } else {
    [textField resignFirstResponder];
    }
    return NO;
    
    }



Swift:


    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        let myTextField = textField.tag + 1
        let nextResponder = textField.superview?.viewWithTag(myTextField) as UIResponder!
        
        if nextResponder != nil {
            nextResponder?.becomeFirstResponder()
        } else {
            textField.resignFirstResponder()
        }
        
        return false
    }

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Saved Image in document directory and save path in coredata not able to fetch the image file iOS swift