How and where would you use instantiateViewControllerWithIdentifier

可紊 提交于 2019-12-23 13:01:19

问题


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.


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/9666222/how-and-where-would-you-use-instantiateviewcontrollerwithidentifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!