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

Programmatically load ViewController from Storyboard in app with multiple storyboards

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

在点击 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

我已尝试遵循此 post here 的建议,但没有任何效果。如何加载此故事板?

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

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

新建

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

试试这个:

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

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

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:Home bundle:nil];

yourDestinationViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@homeStoryboard]; 

[self presentViewController:vc animated:YES completed:^{}];
代码中的

yourDestinationViewController 是你的视图 controller.h 文件。

您想在您的程序中导入那个视图controller.h文件,您希望在其中以编程方式调用视图控制器。

并创建如下所示的 属性

#import "viewcontroller.h"

@property viewController *yourDestinationViewController;