Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Segue in Swift

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 suppose I need to call it manually. Do you have an idea ?

I still tried something like this :

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject) {
    if checkLog == true
    {
     //finish   
    }
    else
   {
    // undo
   }
}

@IBAction func ConexTAP(sender: UIButton)
{
 //check login
}
like image 270
1L30 Avatar asked Jul 07 '14 15:07

1L30


2 Answers

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.

like image 126
mxb Avatar answered Nov 03 '22 18:11

mxb


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.

like image 7
Pulkit Talwar Avatar answered Nov 03 '22 18:11

Pulkit Talwar