Ios – How and where would you use instantiateViewControllerWithIdentifier


UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                     bundle: nil];

MenuScreenViewController *controller = (MenuScreenViewController*)[mainStoryboard 
                                               instantiateViewControllerWithIdentifier: @"<Controller ID>"];

Where exactly do i write this code if i have to make sure that the current view is instantiated with the identifier? Which means if i write any code on this class it has to appear when this viewcontroller loads? Also how would i use it? I dont want to create an instance of the menuscreenviewcontroller. WHich means i have to say self but i used self.view and that doesnt work.

Best Solution

You need to push or present the view controller that you have created. You can not directly change views of the controllers by instantiating.

For example you need to use this code to trigger the transition (maybe a button action):

MenuScreenViewController* controller = (MenuScreenViewController*)[ourStoryBoard instantiateViewControllerWithIdentifier:@"<Controller ID>"];

controller.controlFlag = YES;
controller.controlFlag2 = NO; // Just examples

//These flags will be set before the viewDidLoad of MenuScreenViewController
//Therefore any code you write before pushing or presenting the view will be present after 

[self.navigationController pushViewController:controller animated:YES];
// or [self presentViewController:controller animated:YES];
Related Question