Baker2795
Baker2795

Reputation: 247

Need to call the same function twice but am getting 'invalid redeclaration' error

func textFieldShouldReturn(_ textfield: UITextField) -> Bool{
        p1s1TextField.resignFirstResponder()
        return true
}

func textFieldShouldReturn(_ textfield: UITextField)-> Bool{
        p2s1TextField.resignFirstResponder()
        return true
}

So this is the code I need to write, the only difference being that they affect two different text fields. I understand I need to change the sender in order to avoid the redeclaration error but am unsure what to change it to.

Upvotes: 0

Views: 700

Answers (2)

Bista
Bista

Reputation: 7893

Try this to use delegate method according to different Text-fields.

func textFieldShouldReturn(_ textfield: UITextField) -> Bool{
    if textfield == p1s1TextField {
        p1s1TextField.resignFirstResponder()
    }else if textfield == p2s1TextField {
        p2s1TextField.resignFirstResponder()
    }
        return true
}

Upvotes: 1

Rashwan L
Rashwan L

Reputation: 38833

Just use one of them and the parameter textfield is your current textfield.

So:

func textFieldShouldReturn(_ textfield: UITextField) -> Bool {
        textfield.resignFirstResponder()
        return true
}

Upvotes: 4

Related Questions