iOS:从Storyboard之外的编码UIViewController调用segue

时间:2014-07-10 18:22:37

标签: ios uiviewcontroller storyboard

我有一个自定义的UIVieController类,它没有包含在任何StoryBoard中,我需要从这个类中调用一个包含在StoryBoard中的UIViewController,我尝试了很多方法来做到这一点,但它不起作用,总是得到一个NSException,因为当我尝试实例化StoryBoard的UIViewController并获得一个nil对象时。

我不知道为什么会发生这种情况,我真的很感激任何帮助。

谢谢。

为了澄清这段代码,我省略了将视图推送到UINavigationController。

我试过了。

+(void)loadSocialMediaAppSegue:(AXISAppDelegate*)delegate withName:(NSString*)segueId{


    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    AXISWebViewViewController *axisWebView = [storyboard instantiateViewControllerWithIdentifier:segueId];    

}

和这个

+(void)loadSocialMediaAppSegue:(AXISAppDelegate*)delegate withName:(NSString*)segueId{

    AXISWebViewViewController *axisWebView = [delegate.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:segueId];

}

2 个答案:

答案 0 :(得分:1)

好的,您需要做的第一件事就是在Storyboard中为视图控制器添加identifier,您可以打开Storyboard的Identity Inspector并填充storyboard id字段。

现在你可以从任何你想要的地方调用这个UIViewController:

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"the name of your storyboard" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"the id of your view controller"];

我希望这可以帮到你。

答案 1 :(得分:0)

请发布您正在使用的代码,以从故事板中实例化VC。

一般来说:

从StoryBoard实例化视图控制器:

MyViewController *MyVC = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"MyViewController"];

确保将Storyboard ID和Restoration ID和Class name设置为“MyViewController”或您想要调用它的任何内容,并将其用作instantiateViewControllerWithIdentifier的参数:

然后,如果您在导航控制器中,只需将实例化的视图控制器推送到导航堆栈:

[self.navigationController pushViewController:MyVC animated:YES];

或以模态呈现:

[self presentViewController:MyVC animated:YES completion:nil];