Arbab Rizvi
Arbab Rizvi

Reputation: 177

Simple yet impossible - invoking a function based on tap event from view swift

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?

Upvotes: 0

Views: 118

Answers (1)

Cezary Wojcik
Cezary Wojcik

Reputation: 21845

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).

Upvotes: 2

Related Questions