![](/img/trans.png)
[英]Swift 4 - UITapGestureRecognizer selector function not calling
[英]Selector function is not calling in iOS Swift
在第一個控制器中,“內部發送操作”按鈕添加了通知發布方法
let notificationhide = Notification.Name("hideLiveBtn")
NotificationCenter.default.post(name: notificationhide, object: nil)
然后第二個相機控制器調用通知在視圖didload中添加觀察者方法
NotificationCenter.default.addObserver(self, selector:
#selector(self.hideliveBtn(notification:)), name: notificationhide, object: nil)
選擇器功能:
@objc func hideliveBtn(notification : NSNotification){
btnLiveSettings.isHidden = true
}
在為添加觀察者保留斷點時,它是觀察者,但不調用hideliveBtn函數。
請通過其他方式實現NSNotification中心。
第1步:
extension NSNotification.Name {
public static let hideLiveBtnNotificationKey = NSNotification.Name(rawValue: "hideLiveBtn")
}
第2步
在控制器的viewdidLoad()中
NotificationCenter.default.addObserver(self, selector: #selector(notificationReceived), name: .hideLiveBtnNotificationKey, object: nil)
第三步
選擇器功能
@objc func hideliveBtn(notification : NSNotification){
btnLiveSettings.isHidden = true
}
第四步
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver("hideLiveBtnNotificationKey")
}
我檢查了您的代碼及其運行情況。
class ViewController: UIViewController {
let notificationhide = Notification.Name("hideLiveBtn")
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector:#selector(self.hideliveBtn(notification:)), name: notificationhide, object: nil)
NotificationCenter.default.post(name: notificationhide, object: nil)
}
@objc func hideliveBtn(notification : NSNotification){
print("notification fired")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.