我正在尝试将 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:
我总是出错:
'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 的框架
最佳答案
你在这里犯了错误,你设置的 MainStoryboard01
是 Storyboard Identifer
使用它你可以调用 instantiateViewControllerWithIdentifier
。它不是另一个 Storyboard名称。所以它应该只是这样。
//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/