3
votes

I have 2 storyboard files in my app and I'd like to transition between a ViewController in one to a ViewController in the other. I've hooked up an IBAction in response to a button press on the first ViewController, which calls a method in the AppDelegate. I have verified that this signal reaches the AppDelegate method.

Here is the relevant method I have in the AppDelegate, however, no transition occurs. Can anyone tell me why, or is it a silly idea to have 2 storyboards?

-(void) presentSecondViewController {

UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController* mainViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"main_viewcontroller"];

UIStoryboard* secondStoryboard = [UIStoryboard storyboardWithName:@"SecondStoryboard" bundle:nil];
UIViewController* secondViewController = [secondStoryboard instantiateViewControllerWithIdentifier:@"second_viewcontroller"];

[mainViewController presentViewController: secondViewController animated:YES completion: NULL];

}

1

1 Answers

8
votes

You create a second instance of the initial view controller of the first storyboard. That instance was never shown on the screen as it is different from the one being already shown and thus probably won't show your second view controller. You need the instance of the view controller already being shown. The best way would be to change your implementation to

-(void) presentSecondViewControllerFromViewController:(UIViewController *)sourceController
{
    UIStoryboard* secondStoryboard = [UIStoryboard storyboardWithName:@"SecondStoryboard" bundle:nil];
    UIViewController* secondViewController = [secondStoryboard instantiateViewControllerWithIdentifier:@"second_viewcontroller"];

    [sourceController presentViewController: secondViewController animated:YES completion: NULL];
}

and call it by passing the view controller that contains the button.