ios - 在动态框架中使用 Storyboard

标签 ios swift frameworks storyboard viewcontroller

我正在尝试用 swift 创建一个动态框架。 我的框架在 Storyboard 中包含一个 ViewController。 当推送通知进入我的框架时,我无法打开我的 ViewController。 不过我尝试使用下面的代码:

let bundle = Bundle(identifier: "my_Framework_bundleID")
let frameworkStoryboard: UIStoryboard = UIStoryboard.init(name: "MyFrameworkStoryboard", bundle: bundle)
let viewController = frameworkStoryboard.instantiateViewController(withIdentifier: "MyViewController")
self.present(viewController, animated: true, completion: nil)

这样做只会执行 MyViewController 的 viewDidLoad 方法中的代码,但不会显示加载 UI。

我也尝试过这个:

let bundle = Bundle(identifier: "my_Framework_bundleID")
let controller = UIViewController(nibName: "MyViewController", bundle: bundle)
present(controller, animated: true, completion: nil)

这样做我收到以下错误:

Attempt to present UIViewController: 0x135124920 on MyFramework.anotherViewController: 0x133df1a40 whose view is not in the window hierarchy!

self.pushviewController()self.show() 也不起作用。

我被这个问题困扰了两天。

最佳答案

我找到了让它发挥作用的方法

在我使用框架的应用程序的 Appdelegate 中,我传递了 rootviewcontroller 的实例,如下所示:

test.myFunc(withRootView: (self.window?.rootViewController)!)

现在,在我的框架中,我创建了一个 xib 和一个相应的 UIView 文件。 为了导航到这个 UIView 文件,我在“myFunc”函数中使用了下面的代码。

public func myFunc(withRootView rootview: UIViewController)
    let modalView = MyUIViewClass(frame: self.view.bounds)
     if rootview.presentedViewController == nil {
         rootview.view.addSubview(modalView)
     } else {
         rootview.presentedViewController?.view.addSubview(modalView)
     }

关于ios - 在动态框架中使用 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52516287/

相关文章:

ios - Firebase putData 函数

Objective-C 如何检测来自多个 subview 的多点触控

ios - 快速使用一个类的内部函数到另一个类

swift - 如何为长时间按下的 UIButton 创建目标(重复处理程序)?

swift - Xcode 7 从 XCUIElement 转换为无关类型 'String' 在获取 JSON 时总是失败

.net - Microsoft 完整版 .NET Framework 4.8 是否有任何结束日期

ios - 如何在 Alamofire Post 请求后有一个完成处理程序/ block ?

ios - 应用程序在发布版本中崩溃但在调试中不崩溃

java - 在 Spring java 框架中使用 ElasticSearch 的最佳方式

javascript - 带有应用程序展示的着陆页