0

Okay, this sounds like an easy problem but I'm going crazy trying to solve it.

I'm using the Model View Controller design in my code.

I have a view which has an image view. I add a gesture recognizer to the image view as follows:

 let closeSelector: Selector = "closeView:"
    let gestureRecognizer = UITapGestureRecognizer(target: self, action: closeSelector)
    gestureRecognizer.numberOfTapsRequired = 1
    escapeIcon.addGestureRecognizer(gestureRecognizer)

now when the image or "escape icon" as i've defined it, is tapped, the selector 'closeView" function is called.

However, this function is only called IF I define the closeView function within my view file.

But.. I need to call this function in my view controller file since from within the view controller file there are some additional properties and methods that I want to change.

Any help?

Arbab Rizvi
  • 177
  • 1
  • 2
  • 10
  • Possible duplicate of [UIGestureRecognizer on UIImageView](http://stackoverflow.com/questions/3907397/uigesturerecognizer-on-uiimageview) – Craig Siemens Feb 21 '16 at 23:30
  • So that code is in a UIView subclass, right? – pbasdf Feb 21 '16 at 23:41
  • Yes, I have a new class called "CardView" which inherits from UIView. in the init method I initalize the image view, add the tap recognizer to it and add the imageview as a subview to my card view. I then define the target as the viewcontroller and the selector is also defined in the viewcontroller file. However when I tap on the image the application crashses with error sayin unrecognized selector sent to clase – Arbab Rizvi Feb 22 '16 at 01:07
  • If you are using MVC, then it is **okay** to put the gesture recognizer code in the view controller because that follows the target-action pattern within MVC. – tktsubota Feb 22 '16 at 04:55
  • @ArbabRizvi You need to add another property to your CardView class, say `parentViewController` as per Cezary's answer. In your view controller, when you create the instance of `CardView`, use `myCardView.parentViewController = self` (if the `CardView` instance is created in your storyboard, put that code in `viewDidLoad`.) – pbasdf Feb 22 '16 at 08:34

1 Answers1

2

In UITapGestureRecognizer(target: self, action: closeSelector), the target is the class that the selector is in. If you have a reference to parentViewController or similar, you can just say UITapGestureRecognizer(target: parentViewController, action: closeSelector).

Cezary Wojcik
  • 21,745
  • 6
  • 36
  • 36
  • Hmm, still having issues. I set the target to "viewcontroller.self" and then the action is my selector value. I declare a function in my viewcontrollerfile with the same name as my selector and then when I tap on the icon during the simulation the app crashes saying "unrecognized selector sent to class..." – Arbab Rizvi Feb 22 '16 at 00:56
  • 1
    @ArbabRizvi `viewcontroller.self`? You have to find the instance of the view controller and use that as the target. – tktsubota Feb 22 '16 at 04:54