Swift 學習之 NotificationCenter

來源:互聯網
上載者:User

標籤: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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.