iOS-NotificationCenter addObserver"UIMenuControllerWillHideMenu" [英] iOS - NotificationCenter addObserver "UIMenuControllerWillHideMenu"

查看:168
本文介绍了iOS-NotificationCenter addObserver"UIMenuControllerWillHideMenu"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为UIMenuControllerWillHideMenu添加了通知观察器,但是它没有调用与通知中心添加/关联的选择器.

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

UIMenuControllerWillHideMenuUIMenuController的通知中心标识符,应在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")犯了一个错误,该初始化器可能不需要作为

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"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆