iOS - NotificationCenter addObserver“UIMenuControllerWillHideMenu”

时间:2017-09-07 18:09:37

标签: ios swift observers uimenucontroller notificationcenter

我已为UIMenuControllerWillHideMenu添加了通知观察者,但它没有调用选择器添加/与通知中心相关联。

UIMenuControllerWillHideMenuUIMenuController的通知中心标识符,应在UIMenuController隐藏时调用。但不知怎的,它不起作用。

这是我尝试过的代码(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)
}

无法识别,出了什么问题。

1 个答案:

答案 0 :(得分:0)

通过将NSNotification.Name(rawValue: "UIMenuControllerWillHideMenu")替换为.UIMenuControllerWillHideMenu

来找到解决方案
private func addMenuObserverNotification(){
    NotificationCenter.default.addObserver(self, selector: #selector(self.menuControllerWillHideMenu), name: .UIMenuControllerWillHideMenu), object: nil)
}

通过添加初始化程序NSNotification.Name(rawValue: "UIMenuControllerWillHideMenu")我犯了一个错误,typedef NSString .<value name>可能不需要, FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().add(R.id.fr1, new BlankFragment()).commit(); button = (Button)findViewById(R.id.btn); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { BusStation.getBus().post(new Message("hellworld")); } }); btn2 = (Button)findViewById(R.id.btn2); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { FragmentManager fragmentManager1 = getSupportFragmentManager(); fragmentManager1.beginTransaction().replace(R.id.fr1, new BlankFragment2()).commit(); BusStation.getBus().post(new Message("zhengzhi zhou")); } }); } 直接允许使用{{1}访问预定义值}}

了解更多详情:
NSNotificationName
addObserver:selector:name:object: