Conditional Segue in Swift

后端 未结 2 810
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 05:10

I want to do a \"connexion\" button in my app. And when you tap on it, it will open a new page if you have correct login.

But, I read that we can\'t undo a segue, I

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 06:07

    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.

提交回复
热议问题