码迷,mamicode.com
首页 > 编程语言 > 详细

Swift 学习之 NotificationCenter

时间:2016-12-05 19:01:16      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:als   selector   rem   学习   value   pre   select   blog   sel   

1、发布新通知:

NotificationCenter.default.post(name:  NSNotification.Name(rawValue: "MessageMainCount"), object: nil, userInfo: ["count":"\(count)"])

 2、接受新通知

NotificationCenter.default.addObserver(self, selector: #selector(self.changeBadgeValue(notification:)), name: NSNotification.Name(rawValue: "MessageMainCount"), object: nil)

 3、设置接受通知的监听方法

func changeBadgeValue(notification: Notification) -> Void {
        
        let name = notification.name
        let userInfo = notification.userInfo as! [String:Any]
        let userInfoDic = userInfo as NSDictionary
        
        if name == NSNotification.Name(rawValue: "MessageMainCount"){
 
            let badgeValue: String = userInfoDic.object(forKey: "count") as! String
           
            self.messageVC.parent?.tabBarItem.badgeValue = badgeValue
        }
}

 4、移除通知监听

deinit{
        NotificationCenter.default.removeObserver(self)
}

 

Swift 学习之 NotificationCenter

标签:als   selector   rem   学习   value   pre   select   blog   sel   

原文地址:http://www.cnblogs.com/yxtBlogs/p/6134887.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!