【问题标题】:How to present a view controller from an external framework in swift?如何从外部框架快速呈现视图控制器?
【发布时间】:2016-04-10 03:58:05
【问题描述】:

我已经制作了一个框架,其中包含一个名为“AuthenticationViewController.h”的视图控制器,其 nib 为“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''

【问题讨论】:

    标签: ios objective-c swift frameworks


    【解决方案1】:

    NSBundle(identifier: "SendOTPFramework"),不是NSBundle(path: <#T##String#>)?你确定有可用的标识符吗?你在不同的语言中使用了不同的函数。

    【讨论】:

    • 是“标识符”项目中包含的框架的包标识符。或包含的框架名称。
    • @HussainChhatriwala 框架的包标识符。你也可以在swift中使用path函数。
    • 以上工作能够打开控制器。但应用程序委托几秒钟后仍然会崩溃,没有任何崩溃日志。
    • 看看你在 AuthenticationViewController 委托中做了什么。
    • 制定了一个协议,其中包含成功和失败的两种方法。
    【解决方案2】:

    swift 5 的解决方案,使用类名代替框架标识符:

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

    也不需要将Xib的根视图作为出口连接到文件所有者。

    【讨论】:

      【解决方案3】:

      Xcode 13、Swift 5.5 和 iOS 15 中。

      Framework SDKStoryboard 中的故事板和该故事板中的 vc 名称 HelloLogger

      //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
      

      如果你想要另一种风格How to show View Controller from Framework(SDK) in Swift?

      【讨论】:

        猜你喜欢
        • 2019-09-11
        • 1970-01-01
        • 2016-11-28
        • 2021-10-18
        • 1970-01-01
        • 2018-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多