我正在尝试将用户交互从popOver视图传递给ViewController,然后与WKWebView进行交互。将值传递给WKWebView Swift
目前选定的呼叫 ViewController().callViewControllerMethod()
这种方法正常工作时和不调用callViewControllerMethod()
的酥料饼有按钮其中。
范围内callViewControllerMethod()
是在WKWebView上评估JavaScript的一些代码。当代码运行它打evaluateJavascript
代码并引发"fatal error: unexpectedly found nil while unwrapping an Optional value"
但我不知道为什么 - 这是内evaluateJavascript
功能工作正常,如果直接从ViewController
叫,但前提是直接调用的JavaScript。
任何想法的人?
酥料饼的代码
class BackgroundViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func button1(_ sender: AnyObject!) {
print("button 1 selected")
ViewController().callViewControllerMethod()
}
}
视图控制器代码
class ViewController: UIViewController, WKNavigationDelegate, UISearchBarDelegate {
func callViewControllerMethod() {
print("got to the method")
webView.evaluateJavaScript("document.body.style.background = \"white\";") { (result, error) in
if error != nil {
print(result as Any)
}
else {
print("background code completed")
}
}
}
}
请提供一个代码示例,以确切地说明问题发生的位置。 – paulvs
“但我不知道为什么” - 编译器意外地发现零,同时展开一个可选值。这是在没有提供上下文代码的情况下得到的最佳答案:/ – Danoram
对不起,如果我添加一些代码会有帮助;) – kb12abz