12
新斯威夫特。我有空兩個片段:選擇VS斯威夫特行動4
NotificationCenter.default.addObserver(self,
selector:#selector(ViewController.notificationReceived),
name: Notification.Name(rawValue: name), object: nil)
@objc func notificationReceived(notification:Notification){
let x = notification.userInfo!
print("\(x["name"]!)")
}
最後
let x:UITapGestureRecognizer = UITapGestureRecognizer(target: self,
action: #selector(tapped))
self.addGestureRecognizer(x)
func tapped(){
print("tapped")
self.delegate!.theViewTapped()
}
爲什麼是它的notificationCenter
?我應該提供的selector
參數@objc
標籤,但不是爲UITapGestureRecognizer
動作參數?
究竟是Selector
和Action in
斯威夫特之間的區別?
比較[在Swift 4中,我如何處理@objc推斷棄用#selector()?](https://stackoverflow.com/q/44390378/2976878) - 正如OOPer所說,這兩個示例都需要目標方法是'@ objc'。 – Hamish