Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift prepareForSegue cancel

Tags:

ios

swift

I'm trying to implement a login screen, when login is clicked it does the segue "login".

I added a prepareForSegue() override to try to cancel it if the login fails but I don't see any method to cancel the segue if there is a failure.

What is the best way to do this?

like image 833
Rocky Pulley Avatar asked Mar 05 '15 16:03

Rocky Pulley


3 Answers

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)
}
like image 54
Greg Avatar answered Oct 27 '22 04:10

Greg


You could do a segue from a View Controller, not from a specified button. You do it by ctrl-dragging from the yellow button at top of the VC on storyboard. Remember to give the segue an identifier!

Then you can create an IBAction function from login button and do performSegueWithIdentifier. It should look something like this:

@IBAction func loginButtonTapped(sender: UIButton) {
    if loginSuccess {
        performSegue(withIdentifier: "login", sender: nil)
    }
}

then, in prepare(:), you could do additional setup for the segue, as it will be called before the actual segue takes place.

like image 21
Aleksi Sjöberg Avatar answered Oct 27 '22 05:10

Aleksi Sjöberg


You could use shouldPerformSegue and return false, therefore blocking all segues. For example:

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    return false 
}

Then, once you have a successful login, call performSegue, like this:

performSegue(withIdentifier: "YourSegueIdentifier", sender: nil)

This will ensure that you will only transition to the controller when you have a login success.

like image 1
user13747297 Avatar answered Oct 27 '22 04:10

user13747297