In my rootviewcontroller
i have three tabs below the navigation menu and each tab represents a list of products , in navigation menu i have a cart icon with a number on it to show the number of icons in the cart.
each tab is a seprate viewcontroller
and they all refrenced in rootviewcontroller
like this :
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
let layout = UICollectionViewFlowLayout()
let firstTab = FirstTabController(collectionViewLayout: layout)
let seccondTab = SeccondTabController(collectionViewLayout: layout)
let thirdTab = ThirdTabController(collectionViewLayout: layout)
return [firstTab ,seccondTab ,thirdTab ]
}
I have created a function in defaultviewcontroller
to reset the cart number in navigtion menu but i don't know how to fire this function from firsttabController
This is my function in firsttabcontroller
func AddToCart(sender:UIButton) {
let activityIndicatorView = NVActivityIndicatorView(frame: self.view.frame, type: .circleStrokeSpin, color: .red, padding: 170)
self.view.addSubview(activityIndicatorView)
activityIndicatorView.startAnimating()
let parameters : [String: Any] = [
"productid": sender.tag ,
"quantityid": 1
]
ApiServiceCart.sharedInstance.addProductToCartCatalog(parameters: parameters) { (success) in
activityIndicatorView.removeFromSuperview()
if success == true {
// here i want to fire DefaultController().setupNavigationMenu()
self.displayMessage(Title: MessageTitle.Successfull.rawValue, Message: Message.AddToCart_Successfull.rawValue)
}
else {
self.displayMessage(Title: MessageTitle.Error.rawValue, Message: Message.AddToCart_Error.rawValue)
}
}
}
I tried protocol delegate but it didn't work
github.com/kavehnaseri/Protino/blob/master/Protino . the function i want to call from default controller is (setupNavBarButtonsWithCartOnLeft) wichi is in helper/extension+UIViewController
Finally found the solution ,I should have used NotificationCenter
So , i had a function called setupBarButtonMenu
created in UIViewController
extension and what i wanted to do was just to call it in the ParentViewController from ChildViewController .
i added an observer in ParentViewController like this :
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(self.setupNavBarButtonsWithCartOnLeft), name: NSNotification.Name(rawValue: "CartChanged"), object: nil)
}
and in another function from everywhere i can fire the funtion like this :
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "CartChanged"), object: nil)