ios - 从 Objective-C 初始化 Swift Storyboard

标签 ios objective-c swift xcode storyboard

我正在尝试将 Swift 中的新 Storyboard添加到旧的 Objective-C 应用程序:

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard01" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"OnboardingViewController"];
[self presentViewController:vc animated:YES completion:NULL];

使用 Swift Storyboard:

Onboarding Storyboard

我总是出错:

'Could not find a storyboard named 'MainStoryboard01' in bundle NSBundle <.../Developer/CoreSimulator/Devices/753636C1-ABB5-4D6E-B184-5C4638FB2CE9/data/Containers/Bundle/Application/398410C0-BB2C-4574-B058-95D30F8D1B5D/Credit Call.app> (loaded)'

像这样调用swift类 测试类 *实例 = [新测试类]; [实例测试函数];

正常工作。知道如何在 Objective-C 应用程序中调用 swift Storyboard + 它的 swfit Controller 吗?

//编辑:

我终于开始工作了:

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Onboarding" bundle:nil];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"MainStoryboard01"];

但在下一行

[self presentViewController:vc animated:YES completion:NULL];

我收到这个错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MyAppDelegate presentViewController:animated:completion:]: unrecognized selector sent to instance 0x6080000b3b60'

有什么想法吗?我需要用 Storyboard替换它:

Registration *reg = [[Registration alloc] init];
registrationNav = [[UINavigationController alloc] initWithRootViewController:reg];
[reg release];
[self.mainBackgroundView addSubview:[registrationNav view]];

=> https://codepaste.net/jkfras我添加了 MyAppDelegate.m 的框架

最佳答案

你在这里犯了错误,你设置的 MainStoryboard01Storyboard Identifer 使用它你可以调用 instantiateViewControllerWithIdentifier。它不是另一个 Storyboard名称。所以它应该只是这样。

enter image description here

//You need to put storyboard name here the check the image for more detail
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [sb  instantiateViewControllerWithIdentifier:@"MainStoryboard01"];
[self presentViewController:vc animated:YES completion:nil];

编辑:尝试这种方式。

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *vc = [sb  instantiateViewControllerWithIdentifier:@"MainStoryboard01"];
[self.mainBackgroundView addSubview:[vc view]];

关于ios - 从 Objective-C 初始化 Swift Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41856746/

相关文章:

ios - HealthKit 错误 : Error Domain=com. apple.healthkit Code=100 "Failed to open database"UserInfo= {NSLocalizedDescription=无法打开数据库}

objective-c - 如何发送 swift 选择器引用到 Objective C 属性

ios - 如何将 boost 编译成 .a 库以用于 xcodeproj?

iphone - 如何设置 UISlider Minimum Track Image 使其不会拉伸(stretch)?

ios - Swift 从自定义对象数组中的属性获取第一个字母

swift - 关于在 Swift 中重写类属性的困惑

ios - 文本文件未在 Swift 的 applicationSupport 文件夹中创建

ios - 如何缩放 iOs 5+ 的 UI

objective-c - ios:防止 ScrollView 缩放超出范围

ios - 使用 ABAddressBookRegisterExternalChangeCallback 注册的地址簿更改回调永远不会被调用 (iOS 8)