Reputation: 862
I added a UITapGestureRecognizer
to an element. I want that function called after the tap gesture was recognized is in another class.
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:
@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.
Upvotes: 1
Views: 1538
Reputation: 100533
Since the object is a local variable it's deallocated after functions ends so make it an instance variable inside its class
let selectMoment = SelectMomentClass(countUserMoment: 1, cardView: self.cardView)
Upvotes: 0
Reputation: 1288
Use this.Your method is defined in selectMomentClass
class then you must add target
as selectMomentClass
object and selecter as #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 selectPictureAction(_ sender : UITapGestureRecognizer) {
print("card selectMoment is Tapped")
}
Upvotes: 1