如何在 swift 中加载屏幕后重置 badgeValue?

How to Reset the badgeValue after loading the screen in swift?

如果用户通过访问屏幕看到通知,我想重置 tapItem 的标记值。

使用这段代码,我创建了 badgeValue。但它永远不会被重置:

func createBadgecount() {
    if let tapItems = self.tabBarController?.tabBar.items as NSArray! {
        let tapItem = tapItems[3] as! UITabBarItem
        tapItem.badgeColor = UIColor.black
        tapItem.badgeValue = "\(reports.count)"

    }
}

在此先感谢您的帮助!

您可以在

中设置值为 nil
override func viewDidAppear(_ animated: Bool) {
  if let tabItem = self.tabBarController?.tabBar.selectedItem {
    tabItem.badgeValue = nil
   }
}

如果 UIViewController 确实出现了,您想将 tabBarselectedItembadgeValue 设置为 nil

因此将此添加到 viewDidAppear

override func viewDidAppear(_ animated: Bool) {
    if let tabItem = self.tabBarController?.tabBar.selectedItem {
        tabItem.badgeValue = nil
    }
}