Swift - How to edit outlet in nib while “viewWillDisappear” on ViewController?

I have a two-file project. DetailCollectionViewCell includes a WKWebView that loads specific video and text embedded from the server. My goal is when the user clicks the back button, the video should be stopped. I've tried reloading or setting WebView's URL to nil, but I get Fatal Error and nil value for Web View every time I try to effect detailWebView.

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

DetailCollectionViewCell.swift (Where I populate data for 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: DetailCollectionViewCell {
    func reload() {
        DispatchQueue.main.async{
            self.collectionView.collectionViewLayout.invalidateLayout()
        }
    }
    
    func scrollAuthorAllColumns() {
        collectionView.scrollToItem(at: IndexPath.init(row: 0, section: 1), at: .top, animated: true)
    }

I have 3 files in total; PageViewController.swiftDetailCollectionViewCell.swiftDetailCollectionViewCell.xib. I register the nib DetailCollectionViewCell in viewDidLoad on PageViewController with collectionView.register(UINib(nibName: "DetailCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "DetailCollectionViewCell") and show it in CollectionView defined in PageViewController.swift

I also do think this is an instance issue, but I don't know how to work around it. I'd pass data with the delegate if this is a View Cell -> View Controller case, but this is a View Controller -> View Cell case and I don't know how to do what I want.

Can you try this

NSString *script = @"var videos = document.querySelectorAll("video"); for (var i = videos.length - 1; i >= 0; i--) { videos[i].pause(); };"; [webView stringByEvaluatingJavaScriptFromString:script];

Can't you keep a reference to detailWeb instance in PageViewController ?

Then you would stop the video in viewWillDisappear.

Swift - How to edit outlet in nib while “viewWillDisappear” on ViewController?
 
 
Q