1

我有两个 WebView:webViewcustomizerWebView. 这两个 WKWebViews 都由一个尾随约束附加。本质上,当我转到菜单并单击“显示定制器”showCustomizer()或“隐藏定制器”hideCustomizer()时,它会调用相应的函数并显示或隐藏与customizerWebView.

澄清一下,当从附加的NSMenuItems. 但是,show/hideCustomizer()从本质上检测 URL 的观察者调用时 - 即。url.contains("#close")- 应用程序在第一行animator()代码崩溃并出现以下错误:Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

ViewController.swift

import Cocoa
import WebKit

class ViewController: NSViewController, WKUIDelegate, WKNavigationDelegate {
    var customizerURLObserver: NSKeyValueObservation?

    @IBOutlet var webView: WKWebView!
    @IBOutlet var customizerWebView: WKWebView!
    @IBOutlet var rightConstraint: NSLayoutConstraint!

    override func viewDidLoad() {
        super.viewDidLoad
        ...
        customizerURLObserver = customizerWebView.observe(\.url, options: .new) { webView, change in
            let url = "\(String(describing: change.newValue))"
            ViewController().urlDidChange(urlString: url) }
    }

    func urlDidChange(urlString: String) {
        let url = cleanURL(urlString)
        if url.contains("#close") { hideCustomizer() }  // Observer call to hide function
    }

    @IBAction func showCustomizerMenu(_ sender: Any) { showCustomizer() }  // These work flawlessly
    @IBAction func hideCustomizerMenu(_ sender: Any) { hideCustomizer() }  // These work flawlessly

    func showCustomizer() {
        let customTimeFunction = CAMediaTimingFunction(controlPoints: 5/6, 0.2, 2/6, 0.9)
        NSAnimationContext.runAnimationGroup({(_ context: NSAnimationContext) -> Void in
            context.timingFunction = customTimeFunction
            context.duration = 0.3
            rightConstraint.animator().constant = 280
            customizerWebView.animator().isHidden = false
            webView.animator().alphaValue = 0.6
        }, completionHandler: {() -> Void in
        })
    }

    func hideCustomizer() {
        let customTimeFunction = CAMediaTimingFunction(controlPoints: 5/6, 0.2, 2/6, 0.9)
        NSAnimationContext.runAnimationGroup({(_ context: NSAnimationContext) -> Void in
            context.timingFunction = customTimeFunction
            context.duration = 0.3
            webView.animator().alphaValue = 1     // Found nil crash highlights this line
            rightConstraint.animator().constant = 0
        }, completionHandler: {() -> Void in
            self.customizerWebView.isHidden = true
        })
    }
}

有人可以告诉我为什么这个动画在从 NSMenu 调用时看起来和工作完美无瑕 100 次,但在hideCustomizer()从观察者函数调用一次时崩溃?

我也尝试过调用 NSMenu 对象函数hideCustomizerMenu(self),但无济于事。

4

1 回答 1

1

在线上:

ViewController().urlDidChange(urlString: url)

您错误地创建了视图控制器类的新实例并调用urlDidChange实例。由于这个新实例不是从情节提要/xib 创建的,因此它的所有出口都是 nil,因此当您尝试在其in上调用该animator方法时,它会崩溃,因为它是 nil。webViewhideCustomizer

相反,调用urlDidChangeself实际上是一个弱化的self,这样你就不会创建一个保留循环):

customizerURLObserver = customizerWebView.observe(\.url, options: .new) { [weak self] webView, change in
    let url = "\(String(describing: change.newValue))"
    self?.urlDidChange(urlString: url)
}
于 2020-03-26T23:43:39.590 回答