How to perform a segue only if a condition happens?
What I have tried:
In Main.storyboard I have two view controllers. I have control drag from the View Controller icon of the ViewController.swift to anywhere in the SecondViewController.swift.
I select the arrow with the segue and In Attributes > Identifier I give it the name segueTo2
In ViewController.swift I have a textField1 and the button toTo2. All connected in the storyboard. I also have:
@IBOutlet weak var textField1: UITextField! @IBAction func goTo2(_ sender: UIButton) { performSegue(withIdentifier: "segueTo2", sender: self) } override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool { let str: String? = textField1.text if str!.isEmpty { print ("text field is empty. Do not do the segue") return false } else { return true }
}
The same code works well in macOS (only changing textField1.text for textField1.stringValue). In iOS it does not work. That is, when textField1 is empty it still makes the segue.
How to make the segue only when the Text Field has content?