【问题标题】:Programmatically load ViewController from Storyboard in app with multiple storyboards在具有多个故事板的应用程序中以编程方式从 Storyboard 加载 ViewController
【发布时间】:2016-12-20 18:12:18
【问题描述】:

我正在开发一个应用程序,该应用程序正在过渡到为应用程序的不同部分提供多个故事板。它们通过在适当的时间以编程方式加载它们被链接在一起。

在点击 UIButton 的函数中,它应该以编程方式加载情节提要。代码是这样的:

- (void)loginTapped:(id)sender {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Home.storyboard" bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"homeStoryboard"];
    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentViewController:vc animated:YES completion:NULL];
}

故事板被命名为函数中列出的名称:

并在项目设置中适当地列为目标:

并且有相应的 ID,如下所示:

但是,即使设置了所有这些,我也会收到错误:

'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'Home.storyboard' in bundle NSBundle

我已尝试遵循此帖子 here 的建议,但没有任何效果。如何加载此情节提要?

【问题讨论】:

  • 您可以尝试删除故事板名称中的.storyboard 吗?所以只需storyboardWithName:@"Home"
  • 试试@"Home",让方法找出文件扩展名。
  • @nathan,Phillip 成功了,谢谢!

标签: ios objective-c storyboard uistoryboard ios10


【解决方案1】:

解决方案(如上面的 cmets 所述)是在提供情节提要的名称时不包括 .storyboard

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Home.storyboard" bundle:nil];

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Home" bundle:nil];

【讨论】:

    【解决方案2】:

    试试这个:

     UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Home" bundle:nil];
    

    【讨论】:

      【解决方案3】:

      使用 Objective c 在 ios 中以编程方式调用任何 ViewController:

      UIStoryboard *storyboard = [UIStoryboard storyboardWithName:Home bundle:nil];
      
      yourDestinationViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@homeStoryboard]; 
      
      [self presentViewController:vc animated:YES completed:^{}];
      
      代码中的

      yourDestinationViewController是你的view controller.h文件。

      您希望在程序中导入该 viewcontroller.h 文件,您希望在其中以编程方式调用视图控制器。

      并像下面那样创建 属性

      #import "viewcontroller.h"
      
      @property viewController *yourDestinationViewController;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-04-11
        • 1970-01-01
        • 2018-03-11
        • 2021-08-29
        • 2013-08-09
        • 1970-01-01
        相关资源
        最近更新 更多