Adding tap Gesture that call a function in another class
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKPz5qhCkKGdpPJ0wkSm5JqlliSJoZg92-H4DuCruovhkVFuXerCphZt9wys_Gr3uwQyXObMHDwattjG5BSeCEl_LdDq1GPLct8qYyRyX4fZ9C93oW-TX7IGZLYDwqRgfFCXA4aUxkBAcS/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6VebLk98DYwS3K2nWZSCatab-q4HrSuRk91M4fQA8SHrKU_-x-lq1n780pSU_bIVLqMRv6pc3dhhh-Y2PUwBS9mM0BxvY1eM6XsM-e7Diu4z-wAoeDG0vB3-2_1U9RE8sO0lg4PHiIXbI/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
Adding tap Gesture that call a function in another class
I add a UITapGestureRecognizer
to an element. I want that the function called after the tap gesture was recognized is in another Class.
UITapGestureRecognizer
This is my code :
let selectMoment = SelectMomentClass(countUserMoment: 1, cardView: self.cardView)
selectMoment.draw()
let gesture = UITapGestureRecognizer(target: self, action: #selector(selectMoment.selectMomentAction))
cardView.addGestureRecognizer(gesture)
and my SelectMomentClass
contains my function:
SelectMomentClass
@objc func selectMomentAction(sender : UITapGestureRecognizer)
print("card selectMoment is Tapped")
But when I run the code I have this error:
unrecognized selector sent to instance
Where does my code go wrong? This is the target parameter that I need to change. Can I called a function in another class?
I found a similar question in Objective-C but it does not help me.
SelectMomentClass
UIGestureRecognizer
@dfd Thanks for the comment, I resolved my problem to declare my
SelectMomentClass
like a instance variable– El-Burritos
30 mins ago
SelectMomentClass
2 Answers
2
Since the object is a local variable it's deallocated after functions ends so make it an instance variable inside it's class
let selectMoment = SelectMomentClass(countUserMoment: 1, cardView: self.cardView)
Thanks for your reply, this fix the error message but dont give the message "card selectMoment is Tapped" so I think the function was again not called
– El-Burritos
1 hour ago
you have 2 objcets selectMoment and selectPicture which one do you want ??
– Sh_Khan
1 hour ago
Yes I edit the post I have only one object selectMoment
– El-Burritos
1 hour ago
cardView is UIView or UIImageView ??
– Sh_Khan
1 hour ago
UIView so
isUserInteractionEnabled
is set to true and the error would not be appear if the card have a problem of subview or other..– El-Burritos
1 hour ago
isUserInteractionEnabled
Use this.Your method is defined in selectMomentClass
class then you must add target
as selectMomentClass
object and selecter as #selector(selectMoment.SelectPictureAction)
.
selectMomentClass
selectMomentClass
#selector(selectMoment.SelectPictureAction)
let selectMoment = SelectMomentClass(countUserMoment: 1, cardView: self.cardView)
let gesture = UITapGestureRecognizer(target: selectMoment, action: #selector(selectMoment.SelectPictureAction(_:)))
Method Signature as below in the selectMomentClass.
@objc func selectMomentAction(_ sender : UITapGestureRecognizer)
print("card selectMoment is Tapped")
Same to the Sh_Khan, this solve my error log but don't called the function ...
– El-Burritos
1 hour ago
In have edited my ans. plz check this.
– vivekDas
59 mins ago
thanks, but same issue... this is strange, I don't have any log
– El-Burritos
56 mins ago
in which class you are creating this SelectMomentClass object ?
– vivekDas
54 mins ago
in
class CardCell : UITableViewCell
– El-Burritos
53 mins ago
class CardCell : UITableViewCell
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
I almost spoke abut responder chains and documentation about gesture recognizers not being part of the "normal" chain, but then I clicked on the link you posted and see you haven't post anything related to this part of the answer. (It may not help you, but will help us to help you.) "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." Is
SelectMomentClass
a delegate of the class that has the tap recognizer? If so, look into the responder chain - what you can do forUIGestureRecognizer
.– dfd
39 mins ago