簡體   English   中英

如何快速呈現來自外部框架的視圖控制器?

[英]How to present a view controller from an external framework in swift?

我制作了一個帶有名為“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];

這對我來說很好用。

但是當我在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)

該應用程序因錯誤而崩潰:-

由於未捕獲的異常“NSInternalInconsistencyException”而終止應用程序,原因:“無法加載包中的 NIB:“NSBundle(已加載)”,名稱為“AuthenticationViewController”

NSBundle(identifier: "SendOTPFramework") ,不是NSBundle(path: <#T##String#>)嗎?確定有可用的標識符嗎?您在不同的語言中使用了不同的函數。

Swift 5並使用類名而不是框架標識符的解決方案:

let bundle = Bundle(for: AuthenticationViewController.self)
    let VC = AuthenticationViewController(nibName: "AuthenticationView", bundle: bundle)
    self.present(VC, animated: true, completion: nil)

也不需要將Xib的根視圖作為出口連接到文件所有者。

Xcode 13、Swift 5.5 和 iOS 15 中

來自框架SDKStoryboard 的故事板和該故事板HelloLogger 中的vc 名稱

//Working fine
let s = UIStoryboard(name: "SDKStoryboard", bundle: Bundle(for: HelloLogger.self))
let vc = s.instantiateInitialViewController()!
//self.present(vc, animated: true, completion: nil) //To present VC
self.navigationController?.pushViewController(vc, animated: true) //Push VC

如果你想要另一種風格如何在 Swift 中從框架(SDK)顯示視圖控制器?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM