我有三个 tableViewController
,它们由 tabBarController
管理。并且每个 tabBarItem
都必须像任何其他应用程序一样收到有关过期项目的警报。在我的第一个 tableViewController
中,我尝试在 tabBarItem
的 badgeValue
中显示过期项目计数。这些值来自 fetchedResultsController
的 fetchedObjects.count
,它运行良好。
func tabBarBadgeUpdater() {
for viewController in (self.tabBarController?.viewControllers)! {
let overdue = self.fetchedResultsController.fetchedObjects?.filter({ (record) -> Bool in
return (record.date?.compare(Date()) != .orderedDescending)
})
print(overdue!.count)
if viewController.tabBarItem.tag == 1 {
if overdue!.count != 0 {
DispatchQueue.main.async(execute: {
viewController.tabBarItem.badgeValue = "\(overdue!.count)"
viewController.tabBarItem.badgeColor = UIColor.init(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0)
self.tableView.reloadData()
})
} else {
DispatchQueue.main.async(execute: {
viewController.tabBarItem.badgeValue = ""
viewController.tabBarItem.badgeColor = .clear
self.tableView.reloadData()
})
}
}
}
}
但我的问题是删除它们。当我尝试从模型中删除最后一个对象时,它仍然显示值 1。
如何正确显示TabBarItem
的badgeValue
?
最佳答案
我建议您实现 controllerDidChangeContent
,这是您的 fetchedResultsController
的委托(delegate)方法。每次您的模型更改时都会调用它,您可以查询计数并更新您的徽章编号。
关于swift - 如何正确更新 TabBarItem 的徽章值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41941891/