Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 923
CodeGeek123 Avatar asked Jan 18 '23 06:01

CodeGeek123


2 Answers

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];
like image 113
Ugur Kumru Avatar answered Feb 11 '23 19:02

Ugur Kumru


As per Uğur Kumru's answer, with a small edit: if you are not using a Navigation Controller, and you are developing against iOS 5.0+ you will need to use:

MenuScreenViewController* controller = (MenuScreenViewController*)[ourStoryBoard instantiateViewControllerWithIdentifier:@"<Controller ID>"];
[self presentViewController:controller animated:YES completion:nil];

If you omit the completion:nil you will face errors

like image 33
Rob Avatar answered Feb 11 '23 19:02

Rob