如何以及在哪里使用instantiateViewControllerWithIdentifier

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil]; MenuScreenViewController *controller = (MenuScreenViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: @"<Controller ID>"]; 

如果我必须确保当前视图是用标识符实例化的,我到底在哪里编写这段代码? 这意味着如果我写这个类的任何代码,它必须出现在这个viewcontroller加载? 另外,我将如何使用它? 我不想创buildmenuscreenviewcontroller的实例。 这意味着我必须说自己,但我用self.view,并没有工作。

您需要推送或呈现您创build的视图控制器。 您不能通过实例化直接更改控制器的视图。

例如,您需要使用此代码来触发转换(可能是button操作):

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

按照UğurKumru的回答,用一个小小的编辑:如果你不使用导航控制器,而你正在开发针对iOS 5.0 +,你将需要使用:

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

如果你忽略完成:无你将面临错误