我想发布appDelegate的通知,以便更新tabBarController的徽章编号。
所以在appDelegate:
NSNotificationCenter.defaultCenter().postNotificationName("addBadge", object: nil)
在viewController中:
func addBadge () {
badgeNumber = badgeNumber + 1
var tabArray = self.tabBarController?.tabBar.items as NSArray!
var tabItem = tabArray.objectAtIndex(3) as UITabBarItem
tabItem.badgeValue = "\(badgeNumber)"
}
override func viewWillAppear(animated: Bool) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "addBadge", name: "addBadge", object: nil)
}
没用。 是因为我要触发addBadge func的viewController不活动吗?
答案 0 :(得分:1)
好的找到了解决方案,你必须在init中添加观察者:
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "addBadge", name: "addBadge", object: nil)
}