【问题标题】:How to load a UIViewController from a class that does not inherit UIviewController (from a swift file with class )?如何从不继承 UIviewController 的类(从带有 class 的 swift 文件)加载 UIViewController?
【发布时间】:2019-10-24 10:08:18
【问题描述】:

我正在为一个小部件构建框架。我有两个文件如下: widgetBuilder.swiftwebWidget.swift

widgetBuilder.swift 是带有 getter 和 setter 的文件,它从将要使用这个小部件框架的应用程序中获取某些值。

来自widgetBuilder.swift的代码

import Foundation
class WidgetBuilder {

    private var userId: String!
    private var emailId: String!

    public func setuserId(userId: String) -> WidgetBuilder{
        self.userId = userId
        return self
    } 

    public func setEmailId(emailId: String) -> WidgetBuilder{
        self.emailId = emailId
        return self

    }

    public func build() -> WidgetBuilder{
        // Wanted to load the webview from here 
    }
}

初始化完成后我会调用构建函数,我想加载webWidget.swift的ViewController

来自webWidget.swift的代码

class webWidget: UIViewController, WKUIDelegate, WKNavigationDelegate  {


    var webView: WKWebView!

    override func loadView() {
        webView = WKWebView()
        webView.uiDelegate = self
        webView.navigationDelegate = self
        view = webView
        self.loadWebView()
    }

    public func loadWebView(){
        let url = URL(string: "https://www.google.com")!
        webView.load(URLRequest(url: url))
        webView.allowsBackForwardNavigationGestures = true
    }

    public func loadWidgetScreen() {
        //Something is not correct here  
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let controller = storyboard.instantiateViewController(withIdentifier: "WidgetController") 
        self.present(controller, animated: true, completion: nil)
    }

}

如何从widgetBuilder.swift 加载 webWidget 视图并传递一些数据?

【问题讨论】:

  • 添加导入 UIKit,现在您可以在 widgetBuilder.swift 中使用 uiviewcontroller 对象
  • 我在导入 UIKit 后得到了一个新的:Warning: Attempt to present <UIViewController: 0x7fc71dd1d080> on <widget_wrapper.Widget: 0x7fc71df328d0> whose view is not in the window hierarchy! 这是什么意思?
  • 使用哪个 ref 来呈现视图控制器?你能显示代码吗?
  • 我在 widgetBuilder 中为 webWidget 创建了一个对象并尝试调用loadWidgetScreen()
  • 在这种情况下,您的 webWidget 的视图当前未显示在屏幕上

标签: ios swift xcode11 ios-frameworks


【解决方案1】:

这就是我解决的方法,发布它可能对希望从 swift 文件加载 ViewController 的人有所帮助。

一旦我从我的 swift 文件中调用 build(),它就必须加载视图,

添加了一个var,它从想要加载此小部件的父视图中获取 ViewController 实例。

widgetBuilder.swift里面

private var clientView: UIViewController?

public init(_ viewController:UIViewController){
    self.clientView = viewController;
}

那么,

public func build(){

    // Widget() creates ViewController instance , this viewcontroller is mapped to a widget.xib file
    let controller: UIViewController = Widget() // Creating the instance of the view class
    self.clientView?.addChild(controller) // self.clientView is the ViewController instance that wants to load widget view 
    self.clientView?.view.addSubview(controller.view)
    controller.view.frame = (clientView?.view.bounds)!
    controller.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    controller.didMove(toParent: clientView)
}

【讨论】:

    【解决方案2】:

    在 webWidget.swift 中 用下面替换你的 loadWidgetScreen() 方法

    public func loadWidgetScreen() {
      if var topController = UIApplication.sharedApplication().keyWindow?.rootViewController {
          while let presentedViewController = topController.presentedViewController {
              topController = presentedViewController
          }
    
          // topController should now be your topmost view controller
          let storyboard = UIStoryboard(name: "Main", bundle: nil)
          let controller = storyboard.instantiateViewController(withIdentifier: "WidgetController") 
          self.present(controller, animated: true, completion: nil)
      }
      else {
        print("topViewController not found")
      }
    }
    

    【讨论】:

    • 在 widgetBuilder.swift 中添加import UIKit 对该代码有何影响?它没有在任何地方使用? '
    • 你可以删除它,除非你在 WidgetBuilder 中使用了一些 UIKit 特定的函数/对象
    • self.present(controller, animated: true, completion: nil)实际上只是呈现控制器,并没有全屏显示。
    猜你喜欢
    • 2019-04-13
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多