如何从不继承 UIviewController 的 class 加载 UIViewController(从带有 class 的 swift 文件)?
How to load a UIViewController from a class that does not inherit UIviewController (from a swift file with class )?
我正在为小部件构建框架。我有两个文件如下:
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
}
}
初始化完成后我会调用构建函数,我想加载 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 视图并传递一些数据?
在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")
}
}
这就是我解决的方法,发布它,可能对希望从 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)
}
我正在为小部件构建框架。我有两个文件如下:
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
}
}
初始化完成后我会调用构建函数,我想加载 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 视图并传递一些数据?
在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")
}
}
这就是我解决的方法,发布它,可能对希望从 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)
}