Ios 设置AbbarItem徽章

Ios 设置AbbarItem徽章,ios,swift,parse-platform,uitabbar,uitabbaritem,Ios,Swift,Parse Platform,Uitabbar,Uitabbaritem,我正在使用Swift和Parse.com为iOS 8开发应用程序 我有一个有两个标签的应用程序。第二个选项卡在我的解析数据库中查询一个表,我想根据该解析查询中的对象数更新第二个选项卡的标记值 我了解PFQueries是如何工作的,以及如何检索对象的数量等,但我不确定在哪里(即,以何种方法/功能)放置此查询并更新选项卡项的标记 将使用此代码或类似代码更新徽章值: var tabArray = self.tabBarController?.tabBar.items as NSArray! var t

我正在使用SwiftParse.com为iOS 8开发应用程序

我有一个有两个标签的应用程序。第二个选项卡在我的解析数据库中查询一个表,我想根据该解析查询中的对象数更新第二个选项卡的标记值

我了解PFQueries是如何工作的,以及如何检索对象的数量等,但我不确定在哪里(即,以何种方法/功能)放置此查询并更新选项卡项的标记

将使用此代码或类似代码更新徽章值:

var tabArray = self.tabBarController?.tabBar.items as NSArray!
var tabItem = tabArray.objectAtIndex(1) as UITabBarItem
tabItem.badgeValue = "34" // will use the value of my countObjects Parse query
那么我应该在哪里插入这个代码来更新徽章呢?它需要在应用程序首次加载时更新该值,然后在进入/离开第二个选项卡的视图时更新该值


也许,在通过tabBar访问视图之前,应用程序首次启动时,是否有一个选项卡视图功能可以运行?

您应该关注视图的生命周期和以下三种方法:
viewDidLoad
viewwillbeen
viewwillbeside

func updateBadgeValue() {
    var tabArray = self.tabBarController?.tabBar.items as NSArray!
    var tabItem = tabArray.objectAtIndex(1) as UITabBarItem
    tabItem.badgeValue = "34" // will use the value of my countObjects Parse query
}

// first loads
override func viewDidLoad() {
    super.viewDidLoad()

    updateBadgeValue()
}

//  the view is about to made visible
override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    updateBadgeValue()
}

// the view is about to be removed 
override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)

    updateBadgeValue()
}