UIMenuItem #selector方法在wkwebview中崩溃

UIMenuItem选择器方法在iOS 11 beta SDK中崩溃.

-[WKContentView highlightText]: unrecognized selector sent to instance 0x7f85df8f3200

方法定义:

func highlightText() 
{
  //
}

我尝试在WKWebView中添加UIMenuItem,

let menuItemHighlight = UIMenuItem.init(title: "Highlight", action: #selector(ContentWebkitView.highlightText))
UIMenuController.shared.menuItems = [menuItemHighlight]

最佳答案 当我重写canPerformAction并检查我的自定义选择器时,我也遇到了这个错误.在我的情况下,我想删除除我的自定义菜单项以外的所有菜单项,以下内容使我的工作正常.

class ViewController: UIViewController {

    @IBOutlet weak var webView: MyWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        loadWebView()
        setupCustomMenu()
    }

    func loadWebView() {
        let url = URL(string: "http://www.google.com")
        let request = URLRequest(url: url!)
        webView.load(request)
    }

    func setupCustomMenu() {
        let customMenuItem = UIMenuItem(title: "Foo", action:
            #selector(ViewController.customMenuTapped))
        UIMenuController.shared.menuItems = [customMenuItem]
        UIMenuController.shared.update()
    }

    @objc func customMenuTapped() {
        let yay = "🤪🤪🤪🤪🤪🤪🤪🤪🤪🤪🤪🤪"
        let alertView = UIAlertController(title: "Yay!!", message: yay, preferredStyle: .alert)
        alertView.addAction(UIAlertAction(title: "cool", style: .default, handler: nil))
        present(alertView, animated: true, completion: nil)
    }
}

class MyWebView: WKWebView {
    // turn off all other menu items
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
}
点赞