Adding tap Gesture that call a function in another class

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

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.





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 for UIGestureRecognizer.
– dfd
39 mins ago


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.

Comments

Popular posts from this blog

Executable numpy error

PySpark count values by condition

Mass disable jenkins jobs