如何在Swift 4中更新UIBarButtonItem徽章?

时间:2018-11-17 13:26:00

标签: ios swift

我将徽章放在“ UINavigationBarButton”中。当我进入主屏幕时,它显示适当的值。但是当我执行“ UINavigationBarButton”操作时,我移到了第二个viewcontroller。在这里,我在viewcontroller中调用了API,它给了我计数,在第二个viewcontroller中,我调用了API,这也给了我价值。在家庭视图控制器中,我将值减去,并且该值显示在徽章中。现在,当我从第二个ViewController返回到Home ViewController时,徽章计数将为'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中。

1 个答案:

答案 0 :(得分:1)

代替 viewDidLoad()

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