Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using shouldPerformSegueWithIdentifier( ) method in Swift

I am trying to use swift's shouldPerformSegueWithIdentifier() method, but it accepts 2 arguments. These are (identifier: String!, sender:AnyObject)

My main goal is to execute the code when pressing a login button in my storyboard, and depending of a series of checks return TRUE or FALSE, depending if whether the correct username and password were provided. So here are my questions:

  1. What am I supposed to use as the identifier? Apple's documentation it explains that the identifier is a string that identifies the triggered segue. So suppose that my segue had the name of loginSegueProcess. How could I use it in my ViewController tat is assigned to my UIView? The thing is that I declare the method in my code and it requires me to specify both arguments (identifier & sender). How could I provide the arguments?

  2. Will this method actually fulfill my needs? By that I mean if it will indeed stop the segue transition whenever my Login button is clicked and depending on whether the correct credentials were provided it is going to take you to the next View or it will show, say for example, an AlertView.

Finally, I was thinking that the performSegueWithIdentifier(args) method would help me as well. Does anybody know the difference between them?

Thanks a lot in advance!

like image 835
idelara Avatar asked Feb 12 '23 08:02

idelara


2 Answers

isn't it what you want to do?

override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool {
    if identifier == "LoginSuccessSegue" { // you define it in the storyboard (click on the segue, then Attributes' inspector > Identifier

        var segueShouldOccur = /** do whatever you need to set this var to true or false */

        if !segueShouldOccur {
            println("*** NOPE, segue wont occur")
            return false
        }
        else {
            println("*** YEP, segue will occur")
        }
    }

    // by default, transition
    return true
}
like image 86
Tom - Lunabee.com Avatar answered Feb 14 '23 23:02

Tom - Lunabee.com


You may not invoke shouldPerformSegueWithIdentifier() method by yourself. It will be automatically called just before transition to the next view giving a chance to determine wether the transition should take place or. You may conditionally return YES/NO from this method. If your condition does't involve any sever call,a simple logical checking this method will be enough for you.

performSegueWithIdentifier() is used to invoke a segue programmatically. Consider the above case with a network call, you may return NO from shouldPerformSegueWithIdentifier() initially since authentication is going on. After getting the response from server if it success you can call the segue to execute with performSegueWithIdentifier (Here the identifier is the ID you have given in the storyboard). Before make sure you are supposed to return YES from shouldPerformSegueWithIdentifier().

Now a third case if your segue is connecting from the login button(You have to connect it from the controller itself). The checking of shouldPerformSegueWithIdentifier is no more required. You can just call the segue with performSegueWithIdentifier() after getting the success response from server

like image 34
Anil Varghese Avatar answered Feb 14 '23 21:02

Anil Varghese