1.控制器viewDidLoad方法里注册通知
override func viewDidLoad() {
super.viewDidLoad()
// 3.注册通知,监听菜单
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.change), name: PopoverAnimatorWillShow, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(HomeTableViewController.change), name: PopoverAnimatorWillDismiss, object: nil)
}
2.通知调用的change方法
/**
修改标题按钮状态
*/
func change(){
let titleBtn = navigationItem.titleView as! TitleButton
titleBtn.selected = !titleBtn.selected
}
3.移除通知
deinit{
// 移除通知
NSNotificationCenter.defaultCenter().removeObserver(self)
}
4.通知是如何发送的
let PopoverAnimatorWillShow = "PopoverAnimatorWillShow"
let PopoverAnimatorWillDismiss = "PopoverAnimatorWillDismiss"
// 发送通知,通知控制器即将展开
NSNotificationCenter.defaultCenter().postNotificationName(PopoverAnimatorWillShow, object: self)
// 发送通知,通知控制器即将消失
NSNotificationCenter.defaultCenter().postNotificationName(PopoverAnimatorWillDismiss, object: self)