ios - UIWebView 委托(delegate)不会在自定义类中触发

标签 ios swift delegates

我花了大约 5 个小时在 StackOverFlow 上搜索如何实现它,但我仍然没有找到解决方案!

我创建了一个新的 iOS Swift 应用程序只是为了向您解释我到底想解决什么问题。

首先,我在主 Storyboard 上有一个 UIButton,当您单击它时,它会从类 WebViewAsPopup 中创建一个新对象,该对象以编程方式创建一个新的 WebView 和背景为 UIView,alpha 0.5,添加一些样式,然后将它们作为弹出窗口添加到主视图 Controller ViewController

其次,我无法为那些 WebView 的 设置委托(delegate)来处理 UIWebViewDelegate 并同时收听 webViewDidStartLoadwebViewDidFinishLoad

这是我的代码:

ViewController.swift

//
//  ViewController.swift
//  MySimpleApp
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var showPopupBtn: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func showPopupTapped(_ sender: Any) {
        let popup = WebViewAsPopup()
        popup.showWebViewPopup(on: self)
    }

}

WebViewAsPopup.swift

//
//  WebViewAsPopup.swift
//  MySimpleApp
//

import WebKit

class WebViewAsPopup: NSObject, UIWebViewDelegate {

    func showWebViewPopup(on controller: UIViewController) {
        // Popup background
        let bg = UIView()
        bg.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        bg.layer.backgroundColor = UIColor.black.withAlphaComponent(0.5).cgColor

        // Webview sizing
        let width = UIScreen.main.bounds.width - 60
        let height = UIScreen.main.bounds.height - 200
        let x = UIScreen.main.bounds.width/2 - width/2
        let y = UIScreen.main.bounds.height/2 - height/2

        // Webview stuff
        let webView = UIWebView()
        webView.frame = CGRect(x: x, y: y, width: width, height: height)
        let url = URL(string: "https://google.com")
        let request = URLRequest(url: url!)
        // webView.delegate = self << it crash here
        webView.loadRequest(request)

        // Styling webview popup
        webView.layer.borderWidth = 1
        webView.layer.borderColor = UIColor.black.cgColor
        webView.layer.masksToBounds = true
        webView.layer.cornerRadius = 10

        // Add bg then webview to main view controller
        controller.view.addSubview(bg)
        controller.view.addSubview(webView)
    }

    func webViewDidStartLoad(_ webView: UIWebView)
    {
        print("#webViewDidStartLoad!") // << it doesn't fire :(
    }
    func webViewDidFinishLoad(_ webView: UIWebView)
    {
        print("#webViewDidFinishLoad!") // << it doesn't fire :(
    }

}

最佳答案

正如我在评论中指出的那样,您需要将弹出对象保存在某个地方才能保留它。如果不是(如您的示例代码中所示),那么它将在操作方法 showPopupTapped(_:) 完成后立即被释放。

试试这个:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var showPopupBtn: UIButton!

    let popup = WebViewAsPopup()

    @IBAction func showPopupTapped(_ sender: Any) {
        self.popup.showWebViewPopup(on: self)
    }

}

奖金说明:

如果您想知道为什么不需要对弹出对象中的背景 View 和 Web View 执行相同的操作,那是因为 View 会自动保留它们的 subview 。

因此,一旦您将背景 View 和 Web View 作为 subview 添加到父 View ,它们的保留计数就会增加,并将在当前范围内存活。

关于ios - UIWebView 委托(delegate)不会在自定义类中触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50381630/

相关文章:

ios - 如何在 git 子模块上使用#import <>

ios - 用户输入的数据未保存或显示在表格 View 中

ios - 无法使用以下代码更新记录

ios - 当我使用委托(delegate)将值传递给另一个 ViewController 时,我发现委托(delegate)什么都不做

c# - C#中委托(delegate)的实现

java - Android Java 从后台线程中的异步任务返回结果

ios - swift 3 : How to center horizontally the last row of cells in a UICollectionView with only one section?

ios - 模态视图 Controller 关闭时应用程序崩溃

ios - 应用程序崩溃(段错误 : 11) when turning on voice over

ios - 使用 Swift 将方向设置为纵向