Reputation: 83
I have created custom navigation bar with cart and its count like below code: like this if i call setUpNavigationBar()
in all viewcontrollers i am getting navigationbar with cart and count
func setUpNavigationBar(){
self.navigationController?.navigationBar.backgroundColor = CommonColor.navigationColor
self.navigationController?.navigationBar.barTintColor = CommonColor.navigationColor
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.layer.masksToBounds = false
let cartBtn: BadgeButton = BadgeButton(type: .custom)
cartBtn.setImage(#imageLiteral(resourceName: "icon55"), for: .normal)
cartBtn.addAction(for: .touchUpInside) {
print("in cart")
let signupVC = (StoryBoard.driver).instantiateViewController(identifier: "ShoppingCartVC") as! ShoppingCartVC
self.navigationController?.pushViewController(signupVC, animated: true)
}
cartBtn.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
let cartQty = UserDefaults.standard.value(forKey: "cartCount")
cartBtn.badgeText = cartQty as? String
let btnCart = UIBarButtonItem(customView: cartBtn)
self.navigationItem.setRightBarButton(btnCart, animated: true)
}
func setBadgeCountForCart(with : String?){
let badge = self.navigationItem.rightBarButtonItems?.map({$0.customView}).filter({$0 is BadgeButton}).first as? BadgeButton
badge?.badgeText = with
}
but in ShoppingCartVC
i don't want navigation bar so, i didn't call setUpNavigationBar
in ShoppingCartVC viewdidload
so if remove product from cart and i click back button in ShoppingCartVC
then the cartConut is not updating in poped viewcontroller
after remove product i have updated cartCount like below:
class ShoppingCartVC: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
self.navigationController?.navigationBar.isHidden = true
self.serviceCall()
}
func serviceCall(){
self.cartDB = ShoppingCartDetaModel(dictionary: responseData.dict as NSDictionary? ?? NSDictionary())
UserDefaults.standard.set(self.cartDB?.result?.cart?.total_item, forKey: "cartCount")
self.setUpNavigationBar()
}
@IBAction func back(_ sender: UIButton){
self.navigationController?.navigationBar.isHidden = false
self.serviceCall()
UserDefaults.standard.set(self.cartDB?.result?.cart?.total_item, forKey: "cartCount")
self.setUpNavigationBar()
self.navigationController?.popViewController(animated: true)
}
}
where should i change, please do help
Upvotes: 0
Views: 478
Reputation: 1249
You need to update the BadgeButton
its text
property with the updated quantity after you navigated back. You can use different methods for achieving that. The easiest and less clean one is to call self.setUpNavigationBar()
in the viewWillAppear
method. A cleaner way is to use the NotificationCenter
class or a Publisher
and post an update every time the quantity changes.
ViewWillAppear: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621510-viewwillappear
NotificationCenter: https://developer.apple.com/documentation/foundation/notificationcenter
Publisher: https://developer.apple.com/documentation/combine/publisher
Upvotes: 0