Search code examples
swiftuiviewcontrolleruinavigationcontrolleruipangesturerecognizernavigationcontroller

need to set target method twice in viewcontroller and navigationcontroller - swift - programmatically


There's a NavigationController which has a ViewController as a rootViewController

In this ViewController I declare a panGestureRecogniser like this:

guard let thisNavigationController = navigationController as? ProfileNavigationController else { return }

let panDown = UIPanGestureRecognizer(target: thisNavigationController, action: #selector(handleGesture))
        panDown!.delegate = self
        collectionView.addGestureRecognizer(panDown!)

As you can see I set the target of this gesture to be the navigationController of the viewController that I have previously carefully downcasted to its type.

The method handleGesture has been correctly declared and set to public in its own NavigationController class, in fact in ProfileNavigationController class I have set and declared public:

@objc public func handleGesture(_ gesture: UIPanGestureRecognizer){

However I have this error Cannot find 'handleGesture' in scope in ViewController as if I should declare this method in viewController too even if the target is set to the navigationController.

If I don't set the method also in the ViewController (even if it's empty) the code won't run and I don't know why is that.

If I set it empty in the viewController everything works fine. How can I solve this?


Solution

  • You should use selector of ProfileNavigationController like:

    let panDown = UIPanGestureRecognizer(target: thisNavigationController, action: #selector(ProfileNavigationController.handleGesture))