Search code examples
iosswiftuitapgesturerecognizer

Swift Playgrounds Error: Unrecognized Selector sent to instance (related to UITapGestureRecognizer)


I keep getting the following error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel theUserClicked:]: unrecognized selector sent to instance 0x7fd8c5404380'

I'm not very familiar with programmatically using UITapGestureRecognizers so any help would be great! Not all variables shown

class NoteCard:NSObject {
   let tap:UITapGestureRecognizer
   let NoteCardView:UILabel

 init(cardValue:Int, vc:MainViewController) {
   NoteCardView = UILabel(frame: .zero)
   tap = UITapGestureRecognizer(target: NoteCardView, action: "theUserClicked:")
   NoteCardView.isUserInteractionEnabled = true
   NoteCardView.addGestureRecognizer(tap)
 }
}

class MainViewController:UIViewController {

func theUserClicked(_ recognizer:UITapGestureRecognizer) {
    let card:NoteCard = recognizer.view as! NoteCard
    UIView.animate(withDuration: 0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
        card.NoteCardView.leftAnchor.constraint(equalTo: card.NoteCardView.leftAnchor, constant: (self.elementPlacement/4)+20).isActive = true
        card.NoteCardView.bottomAnchor.constraint(equalTo: card.NoteCardView.bottomAnchor, constant: 100).isActive = true
    }, completion: { (true) in
        self.elementPlacement = self.elementPlacement + 1
        if self.elementPlacement == 5 {
            card.tap.isEnabled = false
        }

    })
}
}

Solution

  • UILabel doesn't have a method declaration matching theUserClicked.

    Instead, move the method theUserClicked in to NotesCardView and pass self to the UITapGestureRecognizer. Then, following the MVC pattern, create a delegate protocol for the MainViewController to conform to. Also, why not just use a UIButton?

    I think you may be slightly confused about the MVC pattern. IMO, it looks like your trying to combine the model and the view.

    I highly recommend reading about the patterns used in iOS. This way you can progress and write better code.

    Here's a description, by Apple, about the MVC pattern: Model-View-Controller

    Here is an example of some code that may help you (May have some errors but, I'm sure you can fix them):

    // Essentially, this is the command line version of the note card, 
    // known as the Model. Based on personal opinion and problem domain, 
    // put your business logic here or use a composition pattern with PO*Os 
    // (Plain old * Language Name * objects).
    
    public class NoteCard: NSObject
    {
        internal(set) var cardValue: Int!
    
        required init(cardValue value: Int)
        {
            cardValue = value
    
            super.init()
        }
    }
    
    // This protocol is used to delegate "actions" to your ViewController.
    public protocol NoteCardViewDelegate
    {
        func noteCardViewTitleLabelDidRecieveTap(_ view: NoteCardView) -> Bool
    }
    
    // This class just represents what things should look like. That's it.
    public class NoteCardView: UIView
    {
        // This is the reference to the UIViewController or whatever may 
        // happen to need to use this view.
        @IBInspectable weak var delegate: NoteCardViewDelegate?
    
        // A content view will come in handy more than likely at some 
        // point. Plus, it's a common pattern to use. You might even want 
        // to use a stackView as well.
        @IBInspectable let contentView = UIView()
    
        // I'm not sure why you want to use a UILabel with a tapGestureRecognizer instead of a UIButton, but that's up to you.
        @IBInspectable let titleLabel = UILabel()
    
        internal var titleLabelTapGestureRecognizer: UITapGestureRecognizer?
    
        override func updateConstraints()
        {
            // add constraints for the contentView and titleLabel
            super.updateConstraints()
        }
    
        // We'll create a common initialization method to follow the DRY 
        // rule (Don't Repeat Yourself). Since we want to be able to use 
        // this view with Interface Builder. This will come in handy. 
        // Especially if we have more than one initialization method.
        internal func commonInit()
        {
            // add label and contentView
    
            self.titleLabelGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTitleLabelTap(_:))
            self.addGestureRecognizer(titleLabelTapGestureRecognizer)
        }
    
        // This method is called if you create an instance of this view in 
        // Interface Builder. It's a good idea to override it and call the 
        // commonInit.
        override func awakeFromNib()
        {
            super.awakeFromNib()
    
            self.commonInit()
        }
    
        override init(withFrame frame: CGRect)
        {
            super.init(withFrame: frame)
    
            self.commonInit()
        }
    
        internal func handleTitleLabelTap(_ recognizer:UITapGestureRecognizer) {
             self.delegate?.noteCardViewTitleLabelDidRecieveTap(self)
        }
    }
    
    public class MainViewController: UIViewController { }
    
    public extension MainViewController: NoteCardViewDelegate
    {
        func noteCardViewTitleLabelDidRecieveTap(_ view: NoteCardView)
        {
            let card: NoteCardView = (recognizer.view as! NoteCardView)
    
             UIView.animate(withDuration: 0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
    
                 card.titleLabel.leftAnchor.constraint(equalTo: card.titleLabel.leftAnchor, constant: (self.elementPlacement / 4) + 20)
                 card.titleLabel.leftAnchor.isActive = true
    
            card.titleLabel.bottomAnchor.constraint(equalTo: card.titleLabel.bottomAnchor, constant: 100)
                 card.titleLabel.leftAnchor.isActive = true
    
            }, completion: { (finished: Bool) in
    
                self.elementPlacement = (self.elementPlacement + 1)
                card.tap.isEnabled = !(self.elementPlacement == 5)
            })
         }
    }