iOS - NotificationCenter addObserver “UIMenuControllerWillHideMenu”

你。 提交于 2019-12-25 11:16:09

问题


I have added notification observer for UIMenuControllerWillHideMenu but it does not call selector added/associated with notification center.

UIMenuControllerWillHideMenu is notification center identifier for UIMenuController and should be called when UIMenuController will hide. But somehow it's not working.

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.


回答1:


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)
}

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>

For more details:
addObserver:selector:name:object:
NSNotificationName



来源:https://stackoverflow.com/questions/46102665/ios-notificationcenter-addobserver-uimenucontrollerwillhidemenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!