0

I have created CustomNavigationController and added some common methods to add UIBarButtonItems to it. Now I want to call those methods from my various viewControllers.

So my question is - How to call methods which belongs to customNavigationController from any other viewController.

I can achieve this in objectiveC by following way :

[(CustomNavigationController *)self.navigationController addLogoutButtonWithVC:self actionLogoutHandler:@selector(actionLogoutHandler)];

where "addLogoutButtonWithVC" is method belongs to CustomNavigationController.

I am trying somewhat in above lines in Swift but no luck.

[Note : I have already replaced NavigationController with CustomNavigationController in storyboard when embedded in, so all navigationControllers are now pointing to CustomNavigationController only]

"Update : Declaration of addLogoutButtonWithViewController and actionLogoutHandler inside CustomNavigationController"

func addLogoutButtonWithViewController(viewCont : UIViewController , selLogout : Selector)  {
    currentController = viewCont
    var barButtonItem : UIBarButtonItem?
    barButtonItem = UIBarButtonItem(image: UIImage(named: "Logout.png"), style: UIBarButtonItemStyle.plain, target: self, action: Selector("actionLogoutHandler"))

   self.navigationController?.navigationItem.rightBarButtonItem = barButtonItem
}

@objc func actionLogoutHandler()  {
    print("Inside logout")
    currentController?.navigationController?.popToRootViewController(animated: true)
}

Any help in this regard is highly appreciated.

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
iLearner
  • 1,670
  • 2
  • 19
  • 45

3 Answers3

1

It should looks like:

if let nav = self.navigationController as? CustomNavigationController { nav.addLogoutButton(withVC: self, actionLogoutHandler:#selector(CustomNavigationController .actionLogoutHandler(_:)) }

CZ54
  • 5,488
  • 1
  • 24
  • 39
  • Thank you!! This is quite close to my answer, but #selector(actionLogoutHandler(_:)) this is not working. In my CutsomNavigationController I have marked actionLogoutHandler as @objC as well. But with your suggested answer compiler is throwing error as "Use of unresolved identifier 'actionLogoutHandler'" – iLearner Jul 25 '18 at 10:31
  • Updated. Can you paste the declaration of `actionLogOutHandler` – CZ54 Jul 25 '18 at 10:39
  • please see my updated question : I have added method declarations from CustomNavigationController – iLearner Jul 25 '18 at 11:00
  • Thank you @CZ54 for your help, but here calling #selector(CustomNavigationController .actionLogoutHandler(_:) is equivalent of #selector(self.actionLogoutHandler(_:)). To access method from another controller "#selector(nav.methodName)" works. – iLearner Jul 27 '18 at 13:04
1

You should try my code as below.

you need object of your navigation controller and use to set in button target.

    if let navigationcontroller = self.navigationController as? CustomNavigationController {
    navigationcontroller.addLogoutButton(withVC: self,
                        actionLogoutHandler:#selector(navigationcontroller.actionLogoutHandler(_:))
}

    @objc func actionLogoutHandler()  {
    print("Inside logout")
    self.popToRootViewController(animated: true)
}
Rakesh Patel
  • 1,673
  • 10
  • 27
  • This is also not working - following is the compiler error : "Argument of '#selector' does not refer to an '@objc' method, property, or initializer" – iLearner Jul 25 '18 at 10:53
  • Thank you!! your answer helped me to access methods of custom class but the error "Argument of '#selector' does not refer to an '@objc' method, property, or initializer" was not suppressed. This answer : https://stackoverflow.com/a/41512372/742815 helped to me to get rid of this error. So I have modified your code snippet which works fine fine for me and can help someone else who encounter same issue. – iLearner Jul 27 '18 at 12:50
0

Try this:

guard let customNavigationController = self.navigationController as? CustomNavigationController else { return }
customNavigationController.addLogoutButtonWithVC()
gorzki
  • 433
  • 3
  • 10