【问题标题】:how to update UIBarButtonItem badge in swift 4?如何在 swift 4 中更新 UIBarButtonItem 徽章?
【发布时间】:2018-11-19 14:20:49
【问题描述】:

我将徽章放在“UINavigationBarButton”中。当我来到主屏幕时,它显示出适当的价值。但是当我执行“UINavigationBarButton”操作时,我会转到第二个视图控制器。在这里,在视图控制器中,我调用了 API,这给了我计数,在第二个视图控制器中,我调用了 API,这也给了我价值。在主视图控制器中,我将值和该值显示到徽章中。现在,当我从第二个视图控制器返回到主视图控制器时,徽章计数将为“0”。这在 ios swift 中怎么可能?

我的 HomeViewcontroller 代码是这样的

var bellButton : MIBadgeButton!
var bellButtonmage : UIImageView!

 override func viewDidLoad() {
    super.viewDidLoad()

    var webcount = 0
    var notificationcount = 0

    if UserDefaults.standard.object(forKey: "webcount") != nil{
        webcount = UserDefaults.standard.object(forKey: "webcount") as! Int
    }
    if UserDefaults.standard.object(forKey: "oldcount") != nil{
        notificationcount = UserDefaults.standard.object(forKey: "oldcount") as! Int
    }

   let remainCount = webcount - notificationcount

    self.countnotificationAPI()

    bellButton = MIBadgeButton.init(frame: CGRect.init(x: UIScreen.main.bounds.size.width-50, y: 0, width: 20, height: 20))
    bellButton.badgeEdgeInsets = UIEdgeInsets.init(top: 5, left: 15, bottom: 0, right: 15)
    bellButtonmage = UIImageView.init(frame: CGRect.init(x: 0, y: 5, width: 20, height: 20))
    bellButtonmage.image = UIImage.init(named: "notification")
    bellButton.addSubview(bellButtonmage)

    bellButton.badgeString = String.init(format: "%d", remainCount)
    bellButton.badgeTextColor = UIColor.white
    bellButton.badgeBackgroundColor = UIColor.red

    let rightbarbutton = UIBarButtonItem.init(customView: bellButton)
   self.navigationItem.rightBarButtonItem = rightbarbutton
    self.bellButton.addTarget(self, action: #selector(notificationaction), for:.touchUpInside)
  }

 @objc func notificationaction() {
    self.performSegue(withIdentifier: "gotonotification", sender: nil)
}

func countnotificationAPI()  {

   let param = ["API" : "notification_count"]

    Alamofire.request(url, method: .post, parameters: param, encoding: URLEncoding.default).responseJSON { (response) in

        if  let datastring = response.result.value  as? [String:Any] {
            let notificationcount = datastring["notification_count"] as! Int
            UserDefaults.standard.set(notificationcount, forKey: "webcount")
            UserDefaults.standard.synchronize()

           }
        }
      }
    }

在SecondViewcontroller代码是

 func notificationAPI()  {

   let param = ["API" : "notification_list"]
     Alamofire.request(url, method: .post, parameters: param, encoding: URLEncoding.default).responseJSON { (response) in

        if  let datastring = response.result.value as? [String:Any] {
            let notificationlist = datastring["notification_list"] as! [[String:Any]]
            let oldcount = datastring["notification_count"]

           UserDefaults.standard.set(oldcount, forKey: "oldcount")
           UserDefaults.standard.synchronize()
               }
              }
            }

我的 navigationBarButton 放在 homeviewcontroller 中。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    而不是 viewDidLoad()

    中尝试您的代码
    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            //update badge count here
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      • 2019-01-29
      相关资源
      最近更新 更多