【问题标题】:Use Storyboards in Dynamic Framework在动态框架中使用情节提要
【发布时间】:2018-09-26 11:05:26
【问题描述】:

我正在尝试快速创建一个动态框架。 我的框架在 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)

这样做我得到以下错误:

尝试在 MyFramework.anotherViewController: 0x133df1a40 上呈现 UIViewController: 0x135124920,其视图不在窗口层次结构中!

self.pushviewController()self.show() 也不能正常工作。

我被这个问题困了两天。

【问题讨论】:

  • 您在哪里调用该代码
  • 如果在 viewController 中运行,您可能需要在主线程中运行。 DispatchQueue.main.async{self.present(viewController, animated: true, completion: nil) }
  • 找出解决方案。感谢 Sh_Khan 和 E.Coms 的帮助

标签: ios swift frameworks storyboard viewcontroller


【解决方案1】:

我想出了一个方法来让它工作

在我使用我的框架的应用程序的 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)
     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 2014-11-18
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 2016-09-17
    相关资源
    最近更新 更多