Question about textfield

Hello,

I have a view have three textfields and a button. I wrote following code to move between textfields using return key.

 func textFieldShouldReturn(_ textField: UITextField) -> Bool 
{
            if textField == self.A {
                self.B.becomeFirstResponder()
            }else if textField == self.B {
                self.C.becomeFirstResponder()
            }
            return true
 }

when I use return key between A->B, above code works properly. but when i use return key between B->C, above code doesn't work. I couldn't figure out what's wrong with this.

if anyone pick me my mistake and suggest solution for it, I'd very appreciate.

Thanks,

c00012

Answered by Claude31 in 788070022

What @darkpaw said, plus:

Are you sure you have set the delegate for A, B (and C eventually).

I tested your code and it works as is as soon as delegate is set to the ViewController.

Note: fields names should start with lowercase.

I'd be wary of comparing objects in that manner. I think maybe TextField has a tag property? You could set A's tag to 0, B's to 1 etc. then check in the function for the value of textfield's tag. Might work.

What @darkpaw said, plus:

Are you sure you have set the delegate for A, B (and C eventually).

I tested your code and it works as is as soon as delegate is set to the ViewController.

Note: fields names should start with lowercase.

Question about textfield
 
 
Q