1
votes

I have a form with multiple inputs inside of FormController. Whenever a user presses the return key, I want it to tab the cursor over to the next textField without closing and reopening the keyboard. However, the textField input is inside of a wrapper class textFieldContainer that adds customization to the text field. This class is also used elsewhere in the project so I don't want to edit this class. I'm new to Swift so I want to know how I can implement the textFieldShouldReturn function from within the FormController class.

I have researched this question and I have the implementation of textFieldShouldReturn down but I do not know where to put it. I think it will involve tags on each of the textFields and cycling through the tags.

2

2 Answers

0
votes

Have you tried something like this? You should be able to assign a tag for those created textfields and then use the followiung code.

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    let txtTag:Int = textField.tag    
    if let next = self.view.viewWithTag(txtTag+1) as? UITextField {
        next.becomeFirstResponder()
    }else{
        textField.resignFirstResponder()
    }

    return true
}

Or something very similar to this should do it.

0
votes

I ended up solving it by creating a delegate for the textFieldContainer with a function called shouldReturnKeyPressTab and then using it in FormController. Then I told shouldReturnKeyPress to use the delegate for the function.