UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle: nil];
MenuScreenViewController *controller = (MenuScreenViewController*)[mainStoryboard
instantiateViewControllerWithIdentifier: @"<Controller ID>"];
如果我必须确保使用标识符实例化当前视图,我在哪里写下这段代码?这意味着如果我在这个类上编写任何代码,它必须在这个viewcontroller加载时出现?我也将如何使用它?我不想创建menuscreenviewcontroller的实例。这意味着我必须说自己,但我使用self.view,但这不起作用。
答案 0 :(得分:7)
您需要推送或显示已创建的视图控制器。您无法通过实例化直接更改控制器的视图。
例如,您需要使用此代码来触发转换(可能是按钮操作):
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];
答案 1 :(得分:2)
根据UğurKumru的回答,通过一个小编辑:如果您没有使用导航控制器,并且您正在针对iOS 5.0+进行开发,则需要使用:
MenuScreenViewController* controller = (MenuScreenViewController*)[ourStoryBoard instantiateViewControllerWithIdentifier:@"<Controller ID>"];
[self presentViewController:controller animated:YES completion:nil];
如果省略完成:nil,您将面临错误