ios - Connect one UIButton to 2 segues

Viewed 6602

I have a UIButton and I am trying to connect it to two segues. Which segue is used depends on a number of conditions.

For example, when the UIButton (with title "next") is pressed,

if(condition 1) then go to screen A. else go to screen B.

I believe my code is correct (I have included it anyways), but the problem is that in the storyboard view, I can connect one button to only one view controller using a segue. How do I fix this ?

Here's the code I'm using -

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
        if([[segue identifier]isEqualToString:@"nextButtonID"]){
            creditCardViewController *cvc = [segue destinationViewController];
        }
        else if([[segue identifier]isEqualToString:@"next2ButtonID"]){
            depositInfoViewController *divc = [segue destinationViewController];
        }
}

-(IBAction)nextClicked{
        if([accountType isEqualToString:@"patron"]){
            [self performSegueWithIdentifier:@"nextButtonID" sender:self];
        }
        else{
            [self performSegueWithIdentifier:@"next2ButtonID" sender:self];
        }
}
4 Answers

In Swift 3:

In the Storyboard, ctrl+drag to create segues from SourceViewController to DestAViewController and DestBViewController, and give each segue an identifier (e.g. "ToDestA" and "ToDestB"). Then:

    @IBAction func onButton(_ sender: Any) {
    if (testForA){
        self.performSegue(withIdentifier: "ToDestA", sender: Any?)
    } else {
        self.performSegue(withIdentifier: "ToDestB", sender: Any?)
        }
    }
Related