Swift - 在 ViewController 上 "viewWillDisappear" 时如何在 nib 中操作插座?

Swift - How to manipulate outlet in nib while "viewWillDisappear" on ViewController?

DetailCollectionViewCell 包括一个 WKWebView,它加载从服务器嵌入的特定视频和文本。我的目标是当用户点击后退按钮时,视频应该停止。我已经尝试重新加载或将 WebView 的 URL 设置为 nil,但每次我尝试影响 detailWebView 时,我都会收到致命错误和 Web 视图的 nil 值。

.
├── _PageViewController.swift
│   └── viewWillDisappear()
├── _DetailCollectionViewCell.swift (File Owner of the nib)
└── └── WKWebView: detailWebView

DetailCollectionViewCell.swift(我在其中为 WebView 填充数据。)

@objc protocol DetailCollectionViewCellDelegate  {
        func reload()
        @objc func scrollAuthorAllColumns()
    }

class DetailCollectionViewCell: UICollectionViewCell {
    @IBOutlet weak var detailWeb: WKWebView!
    var delegate: DetailCollectionViewCell?

    override func awakeFromNib() {
        super.awakeFromNib()

        detailWeb.navigationDelegate = self
        detailWeb.uiDelegate = self;
        detailWeb.autoresizingMask = UIView.AutoresizingMask(rawValue: UIView.AutoresizingMask.flexibleWidth.rawValue | UIView.AutoresizingMask.flexibleHeight.rawValue)
    }

    var itemAuthor: Column? {
        didSet {
            if let column = itemAuthor {
                if(PageViewController.itemId != column.itemId) {
                    let webViewHeight = 50 as CGFloat
                    self.webViewHeight.constant = webViewHeight
                    self.wKWebViewHeight.constant = webViewHeight
                    self.detailWeb.frame.size.height = webViewHeight
                    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                        if(PageViewController.webViewHg <= Int(webViewHeight)) {
                            HUD.show(HUDContentType.progress)
                        }
                    }
                    
                    self.detailWeb.isHidden = true
                    self.delegate?.reload()
                    
                    let HTMLString = "my HTML string"
                    
                    detailWeb.loadHTMLString(String(HTMLString), baseURL: URL.init(fileURLWithPath: Bundle.main.bundlePath))
                }
            }
        }
    }
}

PageViewController.swift

    override func viewWillDisappear(_ animated: Bool) {
        DetailCollectionViewCell.detailWeb.reload() "<--------- Crashes here. detailWebView = nil"
        "Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value"
    }

    extension PageViewController: DetailCollectionViewCellDelegate {
    func reload() {
        DispatchQueue.main.async{
            self.collectionView.collectionViewLayout.invalidateLayout()
        }
    }
    
    func scrollAuthorAllColumns() {
        collectionView.scrollToItem(at: IndexPath.init(row: 0, section: 1), at: .top, animated: true)
    }

我已经使用 方法来实现我的目标。

基本上它的作用是禁用内联播放,只允许 tvPlayer 全屏播放,因此当用户关闭全屏时,视频停止。