想吐槽一下,这个selector一点也不优雅
swift中的方法,一般不需要在前面写@objc,但是selector对应的方法前面必须加。在 Swift 中,默认情况下所有的 Swift 方法在Objective-C 中都是不可见的,所以你需要在这类方法前面加上@objc关键字,将这个方法暴露给 Objective-C,才能进行使用。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.title = "雷霆嘎巴"
self.view.backgroundColor = UIColor(hue: 0.50, saturation: 0.20, brightness: 0.86, alpha: 1.00)
let button = QMUIButton()
button.frame = CGRect.init(x: 10, y: 210, width: 300, height: 50)
button.backgroundColor = UIColor.red
button.addTarget(self, action: #selector(buttonClick), for: UIControl.Event.touchUpInside)
self.view.addSubview(button)
}
@objc func buttonClick(){
print("点击了雷霆")
}
}
@objc
extension ViewController{
func buttonAction() -> Void {
print("点击了雷霆0.0")
}
}
网友评论