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

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

我不断收到以下错误:

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

我不太熟悉以编程方式使用 UITapGestureRecognizers,所以任何帮助都将非常有用! 未显示所有变量

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
        }

    })
}
}

UILabel 没有匹配 theUserClicked.

的方法声明

相反,将方法 theUserClicked 移至 NotesCardView 并将自身传递给 UITapGestureRecognizer。然后,按照 MVC 模式,为 MainViewController 创建一个要遵守的委托协议。另外,为什么不直接使用 UIButton?

我想您可能对 MVC 模式有点困惑。 IMO,您似乎在尝试结合模型和视图。

我强烈建议阅读 iOS 中使用的模式。这样您就可以进步并编写更好的代码。

这是 Apple 对 MVC 模式的描述:Model-View-Controller

下面是一些可能对您有帮助的代码示例(可能有一些错误,但我相信您可以修复它们):

// 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)
        })
     }
}

您的点击手势识别器是 NoteCardView,其 class 是 UILabel。因此 theUserClicked: 操作将发送到 NoteCardView。要解决此问题,请将目标设置为 MainViewController 实例。或者将该动作函数移动到可用的 class 并使 target 成为该 class.

的实例