Reputation: 743
Having just spent a day beating my head against the keyboard, I thought I'd share my diagnosis and solution.
Situation: You add a custom View of custom class CardView
to an enclosing view myCards
in your app and want each card to respond to a tap gesture (for example to indicate you want to discard the card). Typical code you start with:
In your ViewController:
class MyVC : UIViewController, UIGestureRecognizerDelegate {
...
func discardedCard(sender: UITapGestureRecognizer) {
let cv : CardView = sender.view! as! CardView
...
}
In your myCards construction:
cv = CardView(card: card)
myCards.addSubview(cv)
cv.userInteractionEnabled = true
...
let cvTap = UITapGestureRecognizer(target: self, action: Selector("discardedCard:"))
cvTap.delegate = self
cv.addGestureRecognizer(cvTap)
I found the arguments here very confusing and the documentation not at all helpful. It isn't clear that the target: argument
refers to the class that implements discardedCard(sender: UITapGestureRecognizer)
. If you're constructing the recognizer and cards in your ViewController
that's going to be self
. If you want to move the discardedCard
into your custom View class (for example), then replace self
with CardView
in my example, including on the delegate line.
Testing the above code, I found that the discardedCard function was never called. What was going on?
Upvotes: 1
Views: 108
Reputation: 743
So a day later, here's what I had to fix. I hope this checklist is useful to somebody else. I'm new to iOS (coming from Android), so it may be obvious to you veterans:
cv
in the example) has userInteractionEnabled=true
. Note that if you use your own constructor it will be set false by defaultuserInteractionEnabled=true
delegate
statement and addGestureRecognizer
statement made a difference; I didn't find that using Xcode 7.2 and iOS 9.2myCards
container that didn't have the width set correctly and was cutting off the right-most cards (and since clipping is disabled by default, this wasn't visually obvious until I looked in the debugger at the View hierarchy)Upvotes: 1