How to programmatically call a view controller?

I have looked through all the training materials that I can find on this and I still have no answer. I need to call another view from code. I am using UIStoryboards . I changed the view several times by dragging a control from UIButtons , but now it should be from code. I am trying to call up the information page from the main menu if this is the first time the user has opened the application. However, I cannot find a way to change the views from the code. All my views are controlled by the same files ( ViewController2 ). identifier my main menu is ViewControllerMain , and the identifier information page is ViewControllerInfo . First I tried this:

 [ViewControllerMain presentViewController: ViewControllerInfo animated:YES completion: NULL]; 

Then I tried to make different UIViewControllers for each and say:

 [ViewController2 presentViewController: ViewController animated:YES completion: NULL]; 

None of them worked. For the first, he says:

Using an undeclared ViewControllerMain identifier.

In the second, he says:

unexpected interface name "ViewController": expected identifier.

What can I do?

+51
ios objective-c uiviewcontroller uistoryboard presentviewcontroller
Apr 21 '13 at 17:55
source share
8 answers

To create a view controller:

 UIViewController * vc = [[UIViewController alloc] init]; 

To call a view controller (must be called from another view manager):

 [self presentViewController:vc animated:YES completion:nil]; 

For one, use null, not null.




Loading the view controller from the storyboard:

 NSString * storyboardName = @"MainStoryboard"; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil]; UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"]; [self presentViewController:vc animated:YES completion:nil]; 

Identifier identifier of your view controller is either the class name of your view controller or the storyboard identifier that you can assign in the identity inspector of your storyboard.

+117
Apr 21 '13 at 18:29
source share

You need to create an instance of the view controller from the storyboard, and then show it:

 ViewControllerInfo* infoController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerInfo"]; [self.navigationController pushViewController:infoController animated:YES]; 

This example assumes that you have a navigation controller to return to the previous view. You can, of course, also use presentViewController: animated: completion :. The main thing is that your storyboard creates your target view controller using the target view controller identifier.

+18
Apr 21 '13 at 21:00
source share

Swift

This gets the view controller from the storyboard and presents it.

 let storyboard = UIStoryboard(name: "Main", bundle: nil) let secondViewController = storyboard.instantiateViewController(withIdentifier: "secondViewControllerId") as! SecondViewController self.present(secondViewController, animated: true, completion: nil) 

Change the name of the storyboard, the name of the controller of the controller and, if necessary, check the identifier of the controller.

+13
Jun 10 '16 at 4:44
source share

There are two ways to do this:

1, create a segue for your ViewController in your storyboard, as described in my answer here: How to make a session not related to user input in iOS 5?

2, give your ViewController and id and call it using the code in my answer here: Programmatic script call script (no segue needed)?

+3
Apr 21 '13 at 21:16
source share

You can call the ViewController this way if you want with the NavigationController

enter image description here

1. In the current screen: Loading a new screen

 VerifyExpViewController *addProjectViewController = [[VerifyExpViewController alloc] init]; [self.navigationController pushViewController:addProjectViewController animated:YES]; 

2.1 Downloaded: Add below to .h file

 @interface VerifyExpViewController : UIViewController <UINavigationControllerDelegate> 

2.2 Downloaded: Add below to the .m file

  @implementation VerifyExpViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.delegate = self; [self setNavigationBar]; } -(void)setNavigationBar { self.navigationController.navigationBar.backgroundColor = [UIColor clearColor]; self.navigationController.navigationBar.translucent = YES; [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"B_topbar.png"] forBarMetrics:UIBarMetricsDefault]; self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]}; self.navigationItem.hidesBackButton = YES; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Btn_topback.png"] style:UIBarButtonItemStylePlain target:self action:@selector(onBackButtonTap:)]; self.navigationItem.leftBarButtonItem.tintColor = [UIColor lightGrayColor]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Save.png"] style:UIBarButtonItemStylePlain target:self action:@selector(onSaveButtonTap:)]; self.navigationItem.rightBarButtonItem.tintColor = [UIColor lightGrayColor]; } -(void)onBackButtonTap:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } -(IBAction)onSaveButtonTap:(id)sender { //todo for save button } @end 

Hope this will be helpful to someone out there :)

+3
Mar 21 '15 at 12:18
source share

The basic logic of this is: _,

 NSString * storyboardIdentifier = @"SecondStoryBoard"; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: nil]; UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:@"YourviewControllerIdentifer"]; [self presentViewController:UIVC animated:YES completion:nil]; 
+1
Jun 13 '16 at 13:40
source share

Import the view controller class you want to show and use the following code

 KartViewController *viewKart = [[KartViewController alloc]initWithNibName:@"KartViewController" bundle:nil]; [self presentViewController:viewKart animated:YES completion:nil]; 
0
Aug 24 '15 at 7:40
source share
  UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone_iOS7" bundle:nil]; AccountViewController * controller = [storyboard instantiateViewControllerWithIdentifier:@"accountView"]; // [self presentViewController:controller animated:YES completion:nil]; UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController; while (topRootViewController.presentedViewController) { topRootViewController = topRootViewController.presentedViewController; } [topRootViewController presentViewController:controller animated:YES completion:nil]; 
0
Nov 11 '16 at 9:54 on
source share



All Articles