【问题标题】:UITabBar Badge from array.count [duplicate]来自array.count的UITabBar徽章[重复]
【发布时间】:2018-04-27 22:11:47
【问题描述】:

如何快速添加到 array.count 中的 UITabBar Badge 计数?

我有UserDefaults 数组,我想在Badge 中显示可变数组计数的数量,请帮助

【问题讨论】:

  • 重复?严重地?来自数组?
  • @Bogdan59,您只需要从array 获取价值,然后在您的UITabBarController 中设置如下:self.tabBar.items?[2].badgeValue = "12"

标签: ios swift uitabbar badge


【解决方案1】:

这是您需要设置徽章值的视图控制器的类。

class YourViewController: UIViewController {

    static weak var shared: YourViewController?


    override function viewDidLoad() {
        super.viewDidLoad()

        YourViewController.shared = self
    }




   public func updateBadgeValue() {

      guard let array = UserDefaults.standard.value(forKey: "yourKeyOfStoredArray") as? [Any]  else { //raplace yourKeyOfStoredArray to the key you use to store the array

         print("Don't have a stored array for key yourKeyOfStoredArray")
         return
      }

      guard let items = self.tabBarController?.tabBar.items else { // Only if you use tab bar controller, if no delete this scope and uncomment next

         print("Don't have tab bar controller")
         return
       }


       /*
        guard let items = self.tabBar.items  else { // replace tabBar by reffence to your tab bar @IBOutlet

            print("Don't have tab bar")
            return
         }*/



         let index = 0 //<-The index of the tabbar item which you need to set badge value

         if items.count > index {
             items[index].badgeValue = String(array.count)
         } else {
                print("Don't have item at index \(index)")
         }
    }
}

这是视图控制器类,您需要点击一个按钮来更新 YourViewController 的徽章值。

class AnotherViewController: UIViewController {

   @IBAction func buttonPressed() {
       YourViewController?.shared.updateBadgeValue()
   }
}

【讨论】:

  • 好的,这是工作,但是如果在第二个 ViewController 中点击按钮,我如何更改第一个 VC 中的 badge.count?
  • @Bogdan59 好的,但是为什么你没有在问题正文中指定这个?
  • @Bogdan59,查看更新并更新问题。
  • 我更新了你的代码,但是当我点击按钮时 - 另一个 ViewController 中的徽章不会改变数量
  • @Bogdan59,所以向我们展示你正在使用的代码......如果我没有看到代码,我猜不出有什么问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-03
  • 1970-01-01
  • 1970-01-01
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
相关资源
最近更新 更多