【问题标题】:Load View Controller via code通过代码加载视图控制器
【发布时间】:2012-03-06 17:18:47
【问题描述】:

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

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

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

有什么帮助吗?

【问题讨论】:

    标签: objective-c xcode view controller viewcontroller


    【解决方案1】:

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

    UIStoryboard *storybrd = [UIStoryboard storyboardWithName:@"YourStoryBrdName" bundle:nil];
    UIViewController *mycontroller =[storybrd instantiateViewControllerWithIdentifier:@"myIdent"];
    

    现在您应该拥有您的控制器并按照您的喜好展示它。

    【讨论】:

    • 感谢@mit3z 的回复。我不确定我的控制器的名称是什么,我通过情节提要创建了它(如您所见,我是 Xcode 的新手,所以请善待:P)。我给控制器 (myControl) 起了一个名字,但我得到了一个错误。 pastebin.com/9qzjTa7L
    • 再次感谢您的回复。这就是我所拥有的:pastebin.com/ahw0DeLU 但它不起作用。我做错了什么?
    猜你喜欢
    • 2015-07-24
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多