Ray Ray - 3 years ago 76
iOS Question

Swift segue running even though if statment is not true

I am trying to stop the segue running if there is no text in the text field

@IBAction func action(_ sender: Any) {
if( email.text != ""){
emailaddress = email.text!
performSegue(withIdentifier: "segue", sender: self)
}
else
{
print("enter a email")
}
}


The "enter email" prints out in the output but it still performs the segue.

Answer Source

Don't attach your segue to your button, because if the segue is directly attached to the button, the performSegue will perform through the click and not through the line

performSegue(withIdentifier: "segue", sender: self)

Just drag and drop your segue from the yellow icon to the next controller

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download