Search code examples
iosxibgesture

Get reference of gesture recognizer in xib not using IBOutlet


I create a single tap and double tap gesture recognizer in a xib file, and I want to access it in a category, but the Referring Outlet of xib in property and it cannot be inserted in a category, can I use code to access gesture recognizer reference in function viewDidLoad?


Solution

  • Yes you can. A view keeps a list of its Gesture Recognizers so you just need to loop through them.

    for (UIGestureRecognizer *recognizer in self.gestureRecognizers)
    {
        // You need a way to identify it here
    }
    

    In this case, if your category is extending a view controller, you just need to know which view contains the gesture recognizer you want.

    @property(nonatomic, copy) NSArray<__kindof UIGestureRecognizer *> *gestureRecognizers;
    

    https://developer.apple.com/documentation/uikit/uiview/1622542-gesturerecognizers?language=objc

    Edit:

    If your category is extending a ViewController change the loop to self.view.gestureRecognizers (or any other view that you attached it to)