Skip to content

Instantly share code, notes, and snippets.

@MoussaHellal
Created September 1, 2019 21:56
Show Gist options
  • Save MoussaHellal/e01e08ea34fd31fe8cacc5b07d33d2f7 to your computer and use it in GitHub Desktop.
Save MoussaHellal/e01e08ea34fd31fe8cacc5b07d33d2f7 to your computer and use it in GitHub Desktop.
TabBarController
import UIKit
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// add badge to my cart tab icon with value of zero
self.tabBar.items![0].badgeValue = "0"
createObservers()
}
func createObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(TabBarController.setBadge(notification:)), name: addItemNotfication, object: nil)
}
@objc func setBadge(notification: NSNotification) {
//get the existant badge value
let badgeValue = Int(self.tabBar.items![0].badgeValue!)!
// set the new badge value
self.tabBar.items![0].badgeValue = "\(badgeValue + 1)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment