44
votes

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.

8

8 Answers

87
votes

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"];
40
votes

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

In the storyboard give your view controller an identifier (under the Attributes Inspector) then use the following code to bring that view forward.

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"STORYBOARDNAME" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"VIEWCONTROLLERIDENTIFIER"];
[self presentModalViewController:vc animated:YES];
6
votes

I have a case where I want to present a view controller from the main part of the app, one with settings & help & so on. To do this, I want it to be within a nav controller, sort of a little plug in module we can call from a UIBarButtonItem.

Now, this can be to/in the current storyboard, or to another, it doesn't matter.

I want to do it this way, because I loathe the potential of segue line spaghetti all over my storyboard.

Here's how to do it.

- (IBAction)displaySettings:(id)sender
{
    LOG_SELECTOR() // google that for extra goodness

    // FYI, this can be done using a different storyboard like so.
    /*
     NSString * storyboardName = @"MainStoryboard_iPhone"; // possibly use device idiom?
     UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
     */

    // To push a new set of scenes with a new Navigation Controller, it is done like this:
    UINavigationController *settingsNC = [self.storyboard instantiateViewControllerWithIdentifier:@"Settings Nav Controller"];
    OBSettingsUIViewController *settingsVC = [self.storyboard instantiateViewControllerWithIdentifier:@"Settings root"];
    [settingsNC pushViewController:settingsVC animated:NO];

    [settingsNC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

    // Present the view controller;
    [self presentViewController:settingsNC animated:YES completion:NULL];
}

In the presented view controllers (or in a subclass of the Navigation Controller), you can have a UIBarButtonItem to then dismiss the whole presented hierarchy of view controllers like so:

- (IBAction)dismissThisVC:(id)sender {
     [self dismissViewControllerAnimated:YES completion:nil];
}

Hope this helps a bunch of people out. Cheers.

4
votes

enter image description hereJust call viewcontroller using navigation controller Write this code in viewcontroller and set viewcontroller in storyboard as set in the image.

ProfileVC *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ProfileVC"];
[self.navigationController pushViewController:vc animated:YES];
3
votes

Call to navigate to other class

UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];

UINavigationController *navController = (UINavigationController *)window.rootViewController;

DumpFeed *dump = [storyboard instantiateViewControllerWithIdentifier:@"DumpFeed"];

dump.isPushed=YES;

dump.strUserId = appDelegate.strFriendid;


[navController pushViewController:dump animated:YES];
3
votes

Heres a Swift version of this:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myVC = storyboard.instantiateViewControllerWithIdentifier("myStoryId")
self.presentViewController(myVC, animated: true, completion: nil)

You should also change your storyboard id like this: enter image description here

1
votes

I think that with iOS7 it has become very easy implementing via the storyboard

I'm currently learning about the new features in iOS7 and found this simple solution, but it might have been relevant even in prior versions, I'm not sure.

First u need to connect the presenting VC with the target VC (thats the only connection needed), then within the storyboard's attributes inspector choose the style to be modal, in the identity inspector give your VC a storyboardID and make sure you checked the 'use storyboardID' checkbox,

If its not there yet add this method to your presentingVC:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

   YourTargetVC * targetVC = 
         (YourTargetVC *)segue.destinationViewController;

   if(nil != targetVC) {

       //Do preparations here
   }

}

Now, when you wish to show your targetVC from your presentingVC you can use:

[self performSegueWithIdentifier:(NSString *) sender:(id)];

where the identifier is your viewController's storyboardID, and the sender is the view who triggered the action, this method will invoke the storyboards scene, so the [prepareForSegue: sender:] method will be called allowing u making last modifications before the targetViewController will appear.