如何从不继承UIviewController的类(来自具有类的Swift文件)加载UIViewController?

3

我正在构建一个小部件框架。我有两个文件,如下所示:

  • widgetBuilder.swift
  • webWidget.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 
    }
}

完成初始化后,我会调用“build”函数,并想要加载“webWidget.swift”视图控制器的内容。
来自“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视图并传递一些数据?


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

2

这是我解决问题的方法,分享出来,希望能帮助需要从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)
}

0
在 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 对那段代码有什么影响?它似乎没有被使用到? - Mahesh Kumaran
除非您在WidgetBuilder中使用了一些特定于UIKit的函数/对象,否则可以删除该部分。 - Er. Khatri
self.present(controller, animated: true, completion: nil) 其实只是展示了控制器,而没有显示整个屏幕。 - Mahesh Kumaran

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接