9

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];
        }
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
Ashish Agarwal
  • 14,555
  • 31
  • 86
  • 125

4 Answers4

18

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.

Matt S.
  • 13,305
  • 15
  • 73
  • 129
  • But it is doing from the viewController, not from the button. If I understood it correctly. – Anoop Vaidya Feb 19 '14 at 11:00
  • 1
    That's correct. No segues are 'physically' attached to the button. – Matt S. Feb 19 '14 at 13:31
  • The storyboard only lets me ctrl+drag a single segue from it. After one segue has its source as a view controller, i can no longer make that view controller the source for a new segue. Is this new? – Ryan Pierce Aug 17 '17 at 16:08
4

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

        }
}
bhavya kothari
  • 7,484
  • 4
  • 27
  • 53
1

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.

Jeef
  • 26,861
  • 21
  • 78
  • 156
1

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?)
        }
    }
Hyperfine
  • 91
  • 7