Call storyboard scene programmatically (without needing segue)?

狂风中的少年 提交于 2019-11-27 17:17:46
Darren

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

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

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

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.

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:

Bhushan_pawar

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

Just call viewcontroller using navigation controller Write this code in viewcontroller and set viewcontroller in storyboad as set in image.

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

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.

Heres Swift 3 version:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "baseViewController")
self.present(viewController, animated: true, completion: nil)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!