通过代码加载视图控制器

aur*_*r0n 2 xcode controller view objective-c viewcontroller

我有几个ViewControllers的故事板,通过按钮相互连接.

现在,如果特定条件为真,我需要加载另一个UIViewController.我设法创建了一个新的子类,但我想避免它.我只是想

if(condition == true){
  // load viewcontroller located in the storyboard, not connected with anything else
}
Run Code Online (Sandbox Code Playgroud)

有帮助吗?

ksh*_*ksh 5

好的,我会详细说明:使用应该使用类的- (id)instantiateViewControllerWithIdentifier:(NSString *)identifier方法UIStoryBoard,但确保在IB中填写控制器的标识符字段.

UIStoryboard *storybrd = [UIStoryboard storyboardWithName:@"YourStoryBrdName" bundle:nil];
UIViewController *mycontroller =[storybrd instantiateViewControllerWithIdentifier:@"myIdent"];
Run Code Online (Sandbox Code Playgroud)

现在您应该拥有控制器并根据需要显示它.