Ashish Agarwal
Ashish Agarwal

Reputation: 14925

ios - Connect one UIButton to 2 segues

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];
        }
}

Upvotes: 9

Views: 6632

Answers (4)

Hyperfine
Hyperfine

Reputation: 91

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?)
        }
    }

Upvotes: 1

Jeef
Jeef

Reputation: 27275

I've done this often. My method is to make two "manual" segues.

  • In Interface builder ctrl+drag from View Controller 1 to View Controller 2
  • Click on this segue in IB and go to properties and name its Identifier: nextButtonID
  • Create a 2nd segue and follow the same steps naming it next2ButtonID

Then your code should work fine I would think. I've run into issues where I have a segue go from a button - AND THEN - I'm also trying to call a different segue. By having these be general "controller-to-controller" segues they are setup to be called manually as you are doing.

Upvotes: 1

bhavya kothari
bhavya kothari

Reputation: 7474

You don't need to mention any code in prepareForSegue unless if you want to pass some data to other view controller

-(IBAction)nextClicked
{  
    if ([strCheck isEqualToString:@"launch Screen A"])
        {
            UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];

            ScreenA *screenA = (ScreenA *)[storyboard instantiateViewControllerWithIdentifier:@"ScreenA"];
            [self.navigationController pushViewController:screenA animated:NO];

        }
        else
        {

            UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];

            ScreenB *screenB = (ScreenB *)[storyboard instantiateViewControllerWithIdentifier:@"ScreenB"];

            [self.navigationController pushViewController:screenB animated:NO];

        }
}

Upvotes: 4

Matt S.
Matt S.

Reputation: 13763

In the storyboard window, control drag from the view controller icon on the bottom of the view and drag it to the destination view controller, give the segue an identifier, and repeat for the next segue.

Upvotes: 18

Related Questions