48

I just need to know how to make some 'conditional' redirect / segue to some view. For example :

If user haven't login yet, then a view with login form appears. But if user logged in already, they will see their profile view.

How to make conditional segue like that using storyboard?

Thank you

5 Answers 5

80

For conditional segues, don't start a segue from a button as you normally do. Instead of that use the following steps:

  1. In your storyboad, start a segue directly from the source view controller to the destination view controller. For doing this, you can drag your source view controller's icon on the bar just below the view to the destination view controller on the main canvas area. Just remember you have to connect two view controllers directly and not with any control.

  2. Enter a suitable segue identifier for this segue. For example say "conditionSegue"

  3. Now, in your source view controller’s .m file perform your condition and call -performSegueWithIdentifier:sender: method on "self" like this:

    -(void)loadDestinationVC{  
        if(condition == YES){  
    
           [self performSegueWithIdentifier:@"conditionSegue" sender:nil];  
        }  
    }
    

I hope I made it clear.

3
  • It worked thine for me, i did a conditionall segue from a uitableviewcell, because some cells has to show some view and other cells other... The thing is that when i come back from the push segue it has lost focus and reset the view, it wont came back to the cell that started the action. I have a tableview with a segmented control on top that changes between 2 datasources. If i segue from a uitableviewcell with the second index of the segmented control selected, when i come back from the segue, it starts the view with the 1rst index at the segmented control... Apr 14, 2014 at 23:09
  • @fguespe Are you asking a solution to the problem or is it a general comment. Apr 15, 2014 at 1:14
  • yes, i need a solution, but i advanced a bit and i understand is not fault of the conditional segue. for normal segues happens to. The problem is the segmented control that looks for the index when it comes back but it showsme the first segment data. Apr 15, 2014 at 1:58
14

I believe a better approach if you want to use segues from buttons would be to use this:

First create a button and use a segue to connect it to another view, give this segue a name

then use this method in your .m file

    - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    if ([identifier isEqualToString:@"SegueName"]) {
        //Check if should make the segue 
        //if not do something
        [self doSomething];
        //and return NO;
        return NO;
    }
    return YES;
    }
1
  • If you are using xcode 6 and are unsure about which type of sague to use, you can use the "Show" sague under "Action Sague". Also, to give an identifier (name) to the sague, select it in the storyboard and enter name of choice in the "Identifier" field under the "Attributes" inspector (on the right) Jun 9, 2015 at 13:04
8

In Swift you could do : as @Gaurav Singh suggested in his answer

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

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 redirection in main Thread.

Hope this help for someone looking for Swift option

3

You make the segues directly from one controller to another (or others), and give them identifiers. In code you call choose between segues depending on your conditions, and call performSegueWithIdentifier:sender:.

1

Create "generic" segue by ctrl dragging from the view controller icon at the bottom to the destination. This segue won't be associated with any action. Then, in your code where ever the event is using your conditional code that you want to trigger the segue, call:

- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.