以编程方式从具有多个故事板的应用程序中的Storyboard加载ViewController

时间:2016-12-20 18:12:18

标签: ios objective-c storyboard uistoryboard ios10

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

在轻触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];
}

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

storyboard file name

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

storyboard target image

并具有相应的ID:

storyboard id image

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

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

我试图按照这篇文章here的建议,但没有任何效果。如何加载此故事板?

3 个答案:

答案 0 :(得分:5)

解决方案(如上面的评论中所述)在提供故事板的名称时不包括.storyboard

<强>旧

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

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

答案 1 :(得分:2)

试试这个:

{{1}}

答案 2 :(得分:1)

使用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文件,您希望以编程方式调用View控制器。

并创建属性,如下所示,

#import "viewcontroller.h"

@property viewController *yourDestinationViewController;