我正在尝试用 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/