Swift prepareForSegue cancel

You should override shouldPerformSegueWithIdentifier and return false if login failed:

override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
    if let ident = identifier {
        if ident == "YourIdentifier" {
             if loginSuccess != true {
                  return false
             }
         }
     }
     return true
}

UPDATED FOR SWIFT 3
Swift 3 method is now called shouldPerformSegue

    override func shouldPerformSegue(withIdentifier identifier: String?, sender: Any?) -> Bool {
    if let ident = identifier {
        if ident == "YourIdentifier" {
            if loginSuccess != true {
                return false
            }
        }
    }
    return true
}

// Extended

If you programmatically call performSegueWithIdentifier this method will not be called but it’s not need for that, you can call it just your login success, otherwise ignore it:

if loginSuccess {
    performSegueWithIdentifier("login", sender: nil)
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)