Conditional Segue in Swift

陌路散爱 提交于 2019-11-30 19:36:12
mxb

You can do the credentials check in the following method in your ViewController:

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

In alternative, you can bind the UIButton to an action in your view controller, perform the checks here and then trigger a segue by code.

In Swift you can proceed by:

Step 1: Insert a segue from ViewControllerA to ViewControllerB. The segue should start from ViewControllerA itself and not from any control (like Button).

Step 2: Give segue an unique identifier in storyboard. Ex: seageFromAtoB

Step 3 : In your ViewControllerforA.swift write :

if(condition == true) {
    self.performSegueWithIdentifier("seageFromAtoB", sender: self)
}

If you are performing some task in some other Thread then using performSegueWithIdentifier can throw an exception.

If you get this error then you should use :

if(condition==true){
      NSOperationQueue.mainQueue().addOperationWithBlock {
           self.performSegueWithIdentifier("seageFromAtoB", sender: self)
      }
}

This will perform segue from ViewControllerA to ViewControllerB with a specific condition.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!