Using shouldPerformSegueWithIdentifier( ) method in Swift

前端 未结 2 1439
日久生厌
日久生厌 2020-12-11 07:52

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

My main goal is

相关标签:
2条回答
  • 2020-12-11 08:17

    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
    }
    
    0 讨论(0)
  • 2020-12-11 08:27

    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

    0 讨论(0)
提交回复
热议问题