Invoke the scenic scene programmatically (without the need for segue)?

I have a modal scene for a storyboard that I want to be accessible to all my other scenes. Creating a modal transition to it from each scene on my storyboard creates a big mess of lines going everywhere. Is there a way that I leave unchanged and invoke the scene programmatically?

Basically I want to do something like this:

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

except that instead of creating and clicking a view controller class, I want to make a modal transition to an "isolated" (not related to the scene) scene.

+43
ios objective-c xcode uistoryboard
May 09 '12 at 19:38
source share
9 answers

Yes, you can. Do something similar to access VC, then just Modal. Click it:

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil]; MyNewViewController *myVC = (MyNewViewController *)[storyboard instantiateViewControllerWithIdentifier:@"myViewCont"]; 
+86
May 9 '12 at 19:47
source share
β€” -

Note: presentModalViewController method : animated deprecated in iOS 6.

The new code should look like this:

 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]; 
+40
Dec 13 '12 at 18:08
source share

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

 UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"STORYBOARDNAME" bundle:nil]; UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"VIEWCONTROLLERIDENTIFIER"]; [self presentModalViewController:vc animated:YES]; 
+19
May 09 '12 at 19:45
source share

I have a case when I want to present a view controller from the main part of the application, one with settings and help and so on. To do this, I want it to be in the navigation controller, like a small plug-in that we can call from UIBarButtonItem.

Now it can be in / in the current storyboard or in another, it does not matter.

I want to do it this way because I hate the potential of the spaghetti line segue on my entire 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 UIBarButtonItem to then remove the entire presented hierarchy of view controllers, for example:

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

Hope this helps a bunch of people. Greetings.

+6
Jun 26 '14 at 19:04
source share

enter image description here Just call the viewcontroller using the navigation controller. Write this code in the viewcontroller and set the viewcontroller to the storyboad as set in the image.

 ProfileVC *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"ProfileVC"]; [self.navigationController pushViewController:vc animated:YES]; 
+4
Jun 10 '16 at 4:42 on
source share

Call to go to another 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
Jan 28 '15 at 4:24
source share

Here is a version:

 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 identifier as follows: enter image description here

+3
Dec 03 '15 at 8:28
source share

I think that with iOS7 it’s become very easy to integrate through the storyboard

I am currently learning new features in iOS7 and have found this simple solution, but maybe it was relevant even in previous versions, I'm not sure.

First you need to connect the VC presentation to the target VC (that is, the only connection that is needed), then in the storyboard attribute inspector, select a style that will be modal, in the identification inspector, transfer your VC fairy file and make sure that you check the "use storyboardID" checkbox ,

If he has not added this method to your VC view yet:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { YourTargetVC * targetVC = (YourTargetVC *)segue.destinationViewController; if(nil != targetVC) { //Do preparations here } } 

Now that you want to show your target VC from your current VC, you can use:

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

where the identifier is your viewController storyboardID and the sender is the view that triggered the action, this method will call up the storyboard scene, so the [prepareForSegue: sender:] method will be called, allowing you to make the last change before the targetViewController appears.

+1
Sep 28 '13 at 17:28
source share

Heres Swift 3 Version:

 let storyboard = UIStoryboard(name: "Main", bundle: nil) let viewController = storyboard.instantiateViewController(withIdentifier: "baseViewController") self.present(viewController, animated: true, completion: nil) 
0
Dec 25 '16 at 5:56
source share



All Articles