ios - Connect one UIButton to 2 segues

天涯浪子 提交于 2019-12-21 04:05:13

问题


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

回答1:


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.




回答2:


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

        }
}



回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/11553141/ios-connect-one-uibutton-to-2-segues

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!