如何在swift中从外部框架呈现视图控制器?

时间:2023-01-23 22:43:07

I have made a framework with a view controller named "AuthenticationViewController.h" with nib "AuthenticationViewController.xib". And a sample project to test have used to present AuthenticationViewController. In Objective-C:

我用名为“AuthenticationViewController”的视图控制器创建了一个框架。h AuthenticationViewController.xib“用笔尖。一个要测试的示例项目用于显示AuthenticationViewController。在objective - c中:

NSString *frameworkDirPath = [[NSBundle mainBundle] privateFrameworksPath];
NSString *frameworkBundlePath = [frameworkDirPath stringByAppendingPathComponent:@"xxx.framework"];
NSBundle *frameworkBundle = [NSBundle bundleWithPath:frameworkBundlePath];
AuthenticationViewController *authenticationViewController = [[AuthenticationViewController alloc]initWithNibName:@"AuthenticationViewController" bundle:frameworkBundle];
authenticationViewController.delegate = self;
[self presentViewController:authenticationViewController animated:YES completion:nil];

Which works fine for me.

这对我来说没问题。

But when I use following code in Swift:

但是当我在Swift使用以下代码时:

let frameworkBundle = NSBundle(identifier: "xxx")

let authViewControler :AuthenticationViewController = AuthenticationViewController.init(nibName: "AuthenticationViewController", bundle: frameworkBundle)
authViewControler.delegate = self
self.presentViewController(authViewControler, animated: true, completion: nil)

The app crashes with error:-

这个应用程序出错了:-

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle (loaded)' with name 'AuthenticationViewController''

由于未被捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因是:'无法在bundle中加载NIB 'NSBundle(已加载)'并名为'AuthenticationViewController'

1 个解决方案

#1


2  

NSBundle(identifier: "SendOTPFramework"),Not NSBundle(path: <#T##String#>)?Are you sure there is an available identifier?You used different function in different lang.

NSBundle(标识符:“SendOTPFramework”),而不是NSBundle(路径:< T # # #字符串# >)?你确定有可用的标识符吗?你在不同的lang中使用了不同的功能。

#1


2  

NSBundle(identifier: "SendOTPFramework"),Not NSBundle(path: <#T##String#>)?Are you sure there is an available identifier?You used different function in different lang.

NSBundle(标识符:“SendOTPFramework”),而不是NSBundle(路径:< T # # #字符串# >)?你确定有可用的标识符吗?你在不同的lang中使用了不同的功能。