问题描述
我为UIMenuControllerWillHideMenu
添加了通知观察器,但是它没有调用与通知中心添加/关联的选择器.
I have added notification observer for UIMenuControllerWillHideMenu
but it does not call selector added/associated with notification center.
UIMenuControllerWillHideMenu
是UIMenuController
的通知中心标识符,应在UIMenuController
隐藏时调用.但是不知何故,它不起作用.
UIMenuControllerWillHideMenu
is notification center identifier for UIMenuController
and should be called when UIMenuController
will hide. But somehow it's not working.
这是我尝试过的代码(Swift 3.x):
Here is code I've tried (Swift 3.x):
private func addMenuObserverNotification(){
NotificationCenter.default.addObserver(self, selector: #selector(self.menuControllerWillHideMenu), name: NSNotification.Name(rawValue: "UIMenuControllerWillHideMenu"), object: nil)
}
// This function should be called on 'UIMenuControllerWillHideMenu'
func menuControllerWillHideMenu() -> Void {
removeMenuObserverNotification()
}
private func removeMenuObserverNotification(){
NotificationCenter.default.removeObserver(self)
}
无法识别,这是怎么回事.
Unable to identify, what's wrong.
推荐答案
找到了一个解决方案,只需将NSNotification.Name(rawValue: "UIMenuControllerWillHideMenu")
替换为.UIMenuControllerWillHideMenu
Found a solution by replacing NSNotification.Name(rawValue: "UIMenuControllerWillHideMenu")
with just .UIMenuControllerWillHideMenu
private func addMenuObserverNotification(){
NotificationCenter.default.addObserver(self, selector: #selector(self.menuControllerWillHideMenu), name: .UIMenuControllerWillHideMenu), object: nil)
}
我通过添加初始化器NSNotification.Name(rawValue: "UIMenuControllerWillHideMenu")
犯了一个错误,该初始化器可能不需要作为 NSNotificationName 是typedef NSString
,它直接允许使用.<value name>
I did a mistake by adding it's initializer NSNotification.Name(rawValue: "UIMenuControllerWillHideMenu")
, which may not require as NSNotificationName is typedef NSString
, which directly allows an access to predefined values using .<value name>
有关更多详细信息:
addObserver:selector:name:object:
NSNotificationName
For more details:
addObserver:selector:name:object:
NSNotificationName
这篇关于iOS-NotificationCenter addObserver"UIMenuControllerWillHideMenu"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!