How to call a function from another class

kaveh naseri

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

kaveh naseri

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to call a function from another class in python?

From Dev

How to call function from another class anywhere

From Dev

PHP how to call function from another class?

From Dev

How to call a function from another class in python

From Dev

How to call function from another class?

From Dev

how to call a variable from one class function to another class function

From Dev

PHP How to call a function from another class and another file?

From Dev

Call a function from an another class

From Dev

How to call a function of a parent class from another function in jquery

From Dev

How do I call a function that is member of a class, from another function?

From Dev

How to call function from one class from another?

From Dev

How to call function in another class

From Dev

PHP - Call class function from another class

From Dev

How to call another Codeigniter Class/function from other Codeigniter

From Dev

How to call one function(method) from another in a class?

From Dev

Execute function to call performSegueWithIdentifier from another class

From Dev

Execute function to call performSegueWithIdentifier from another class

From Dev

Call a python function from one class in another

From Dev

Call a function in viewcontroller with parameter from another class

From Dev

How to call a class from another class

From Dev

How to call a JavaFX class from another class?

From Dev

PHP - Call function from after call to another function in the class

From Dev

PHP - How to call another function in a class dynamically

From Dev

how to call a function from another function in Jquery

From Dev

How to call a number from another function into a function?

From Dev

How to call from function to another function

From Dev

How to call a function from within another function?

From Dev

How to call function in class to function another class in php?

From Dev

how to call @selector method from another class

Related Related

  1. 1

    How to call a function from another class in python?

  2. 2

    How to call function from another class anywhere

  3. 3

    PHP how to call function from another class?

  4. 4

    How to call a function from another class in python

  5. 5

    How to call function from another class?

  6. 6

    how to call a variable from one class function to another class function

  7. 7

    PHP How to call a function from another class and another file?

  8. 8

    Call a function from an another class

  9. 9

    How to call a function of a parent class from another function in jquery

  10. 10

    How do I call a function that is member of a class, from another function?

  11. 11

    How to call function from one class from another?

  12. 12

    How to call function in another class

  13. 13

    PHP - Call class function from another class

  14. 14

    How to call another Codeigniter Class/function from other Codeigniter

  15. 15

    How to call one function(method) from another in a class?

  16. 16

    Execute function to call performSegueWithIdentifier from another class

  17. 17

    Execute function to call performSegueWithIdentifier from another class

  18. 18

    Call a python function from one class in another

  19. 19

    Call a function in viewcontroller with parameter from another class

  20. 20

    How to call a class from another class

  21. 21

    How to call a JavaFX class from another class?

  22. 22

    PHP - Call function from after call to another function in the class

  23. 23

    PHP - How to call another function in a class dynamically

  24. 24

    how to call a function from another function in Jquery

  25. 25

    How to call a number from another function into a function?

  26. 26

    How to call from function to another function

  27. 27

    How to call a function from within another function?

  28. 28

    How to call function in class to function another class in php?

  29. 29

    how to call @selector method from another class

HotTag

Archive