Reputation: 14925
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
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
Reputation: 27275
I've done this often. My method is to make two "manual" segues.
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
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
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