【问题标题】:iOS: Call segue from coded UIViewController outside of StoryboardiOS:从 Storyboard 之外的编码 UIViewController 调用 segue
【发布时间】:2014-07-11 02:38:11
【问题描述】:

我有一个自定义 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];

}

【问题讨论】:

  • 请分享您尝试过但没有产生预期结果的代码。

标签: ios uiviewcontroller storyboard


【解决方案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"];

希望对你有帮助。

【讨论】:

  • 我只需要将标识符添加到 UIViewController 并且我正在为 segue 创建标识符,所以这就是问题所在。谢谢。
【解决方案2】:

请发布您用于从情节提要中实例化 VC 的代码。

一般:

从 StoryBoard 实例化视图控制器:

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

确保将 Storyboard ID 和 Restoration ID 以及 Class name 设置为“MyViewController”或任何您想要调用的名称,并将其用作 instanceViewControllerWithIdentifier 的参数:

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

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

或以模态方式呈现:

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

【讨论】:

    猜你喜欢
    • 2012-11-08
    • 2013-06-20
    • 1970-01-01
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 2012-02-08
    相关资源
    最近更新 更多