[英]How to use #selector correctly in Swift?
I'm adding a reload button for iOS in-app web browser using WKWebView.reload
我正在使用
WKWebView.reload
为iOS应用内网络浏览器添加一个重新加载按钮
I tried two options, and the two options works for me the same but I want to know what is the technical correct option to use with #selector. 我尝试了两个选项,这两个选项对我来说都一样,但是我想知道与#selector一起使用的技术上正确的选项是什么。 Is it
#selector(class.method)
or #selector (object.method)
? 是
#selector(class.method)
还是#selector (object.method)
?
Here is the first: 这是第一个:
let webView = WKWebView()
view = webView
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(WKWebView.reload))
and here the second one: 这里是第二个:
let webView = WKWebView()
view = webView
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(webView))
Here is the full code 这是完整的代码
import UIKit
import WebKit
class ViewController: UIViewController {
let webView = WKWebView()
override func loadView() {
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: webView, action: #selector(WKWebView.reload))
}
}
Your let webView = WKWebView()
is creating a new instance of yours.. it won't work independently of your #selector
您的
let webView = WKWebView()
正在创建您的新实例..它不会独立于#selector
try it out: 试试看:
func reloadWebView() {
webView.reload()
}
your call: 您的来电:
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(MyViewController.reloadWebView))
The function reload
is not declared as @objc , therefore, it won't work as selector in swift. 函数
reload
并未声明为@objc ,因此,它不会迅速用作选择器。 You may want to create a function in your ViewController and call the reload function from there. 您可能要在ViewController中创建一个函数,然后从那里调用reload函数。
@objc func reloadWebView() {
webView.reload()
}
and your selector will look like this #selector(reloadWebView)
并且您的选择器将如下所示:
#selector(reloadWebView)
Source: https://developer.apple.com/documentation/webkit/wkwebview/1414969-reload 来源: https : //developer.apple.com/documentation/webkit/wkwebview/1414969-reload
Currently, my recommendation is using #selector(target.method)
. 目前,我的建议是使用
#selector(target.method)
。
When you pass self
as target
then, #selector(self.method)
, passing webView
to target
, then #selector(webView.method)
. 当您将
self
作为target
传递时, #selector(self.method)
,将webView
传递给target
,然后传递#selector(self.method)
#selector(webView.method)
。
#selector
is just an encoded ObjC method name (which does not include class name), so both #selector(ClassName.method)
and #selector(object.method)
should work. #selector
只是一个经过编码的ObjC方法名称(不包括类名称),因此#selector(ClassName.method)
和#selector(object.method)
都应该起作用。 But you can expect a better compile-time diagnostic with #selector(target.method)
. 但是您可以期望使用
#selector(target.method)
更好的编译时诊断。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.