Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call storyboard scene programmatically (without needing segue)?

I have a modal storyboard scene that I want to be accessible to all my other scenes. Creating a modal segue to it from every scene on my storyboard creates a big mess of strings going everywhere. Is there a way that I leave off the segues and call the scene programmatically instead?

Basically I want to do something like this:

  MyNewViewController *myNewVC = [[MyNewViewController alloc] init];
  [self presentModalViewController:myNewVC animated:YES];

except instead of creating and pushing a view controller class, I want to do a modal transition to an "isolated" (not connected with a segue) storyboard scene.

like image 630
zakdances Avatar asked May 09 '12 19:05

zakdances


2 Answers

Yes you can. Do something like this to get access to the VC, then just Modal Push it:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
MyNewViewController *myVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:@"myViewCont"];
like image 79
Darren Avatar answered Oct 15 '22 20:10

Darren


Note: the method presentModalViewController:animated is deprecated in iOS 6.

The new code should read:

NSString * storyboardName = @"MainStoryboard_iPhone";
NSString * viewControllerID = @"ViewID";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
MyViewController * controller = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
[self presentViewController:controller animated:YES completion:nil];
like image 40
Charles Wilson Avatar answered Oct 15 '22 20:10

Charles Wilson