Is it possible to cancel a segue in the prepareForSegue:
method?
I want to perform some check before the segue, and if the condition is not true (in this case, if some UITextField
is empty), display an error message instead of performing the segue.
Its easy in the swift .
As Abraham said, check valid or not in the following function.
And, the
performSegueWithIdentifier:sender:
called by programming can be blocked by overwriting following method. By default, it is not checking valid or not by-shouldPerformSegueWithIdentifier:sender:
, we can do it manually.Should Perform Segue for Login Register
Similar to Kaolin's answer is to leave the seque wired to the control but validate the control based on conditions in the view. If you're firing on table cell interaction then you also need to set the userInteractionEnabled property as well as disabling the stuff in the cell.
For instance, I've got a form in a grouped table view. One of the cells leads to another tableView that acts as a picker. Whenever a control is changed in the main view I call this method
Swift 3: func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool
Return value true if the segue should be performed or false if it should be ignored.
Example:
Swift 4 Answer:
Following is Swift 4 implementation to cancel segue: