Alex Cauthen Alex Cauthen - 1 year ago 56
Swift Question

Swift: Keep segue from executing if condition not met

Currently I'm working on two different ViewControllers in my application, we can call them VC1 and VC2. VC1 displays a registration form, with a question in each row of a UITableView, and has an edit button. The edit button is as a segue from VC1 and VC2. VC2 displays an interface that will allow the user to edit the question. I want to write the application so that you cannot segue from VC1 to VC2 unless you have selected a question(row) to edit. Right now I have some of it set up, but it's possible that I have it backwards.

In VC1:

// this method isn't called until VC2 segues back to VC1... so I don't know how
// to make sure that a selection has been made before pressing the button
@IBAction func editQuestion(segue:UIStoryboardSegue) {
if let editQuestion = segue.sourceViewController as? VC2 {


In VC2:

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject!) -> Bool {

// check to make sure that user has completed all fields of question
// if they haven't completed all fields, return false (do not segue)
// else segue to VC1

Answer Source

Implement in VC1:

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
        if identifier == <your storyboard segue identifier> {


Give your storyboard segue an identifier, and then you can do the check in VC1, and return false if you do not want the segue to happen.

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