Simply I want to add a tap gesture for a UIImageView
that when the user touches, calls a method that is implemented in another class (not the same class that contains the UIImageView
).
Can I do this and if so how can i do it ?
Simply I want to add a tap gesture for a UIImageView
that when the user touches, calls a method that is implemented in another class (not the same class that contains the UIImageView
).
Can I do this and if so how can i do it ?
You can do that. But you need the instance of the target class (class in which method is going to execute) as delegate or something in the gesture adding class.
Here delegate will be the instance of the target class that you have to set before going to that class.
Edit
I will explain a little more. Suppose you have two view controller classes VCA and VCB. you want to call a method in VCA from VCB through a tap gesture.
in VCB.h
in VCB.m
in VCA.m
You will present/ push VCB
here self.desired view is the view in which you want to add gesture and you should add gesture in following way
and "webViewTapped.delegate = self" means you want to call a function of the same class when user tap and you can change it to your desired function by using delegate like self.delegate
One straightforward approach is calling the method of another class from the selector method of
tapGesture
ie.Add UITapGestureRecogniser to the imageview
Dont forget to enable the
userInteraction
of the UIImageView, because by default for imageview userInteraction is disabled.you can do this like below.
Then in the selector of tapGestureRecogniser call the method of another class
Here
next
is the object of another class. You can use delegation also to call the method.Hope this will help you.
Declare recognizer selector on init
Link to article about selector.