gpt4 book ai didi

ios - Swift Playground 错误 : Unrecognized Selector sent to instance (related to UITapGestureRecognizer)

转载 作者:行者123 更新时间:2023-11-28 15:45:30 24 4
gpt4 key购买 nike

我不断收到以下错误:

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 创建一个要遵守的委托(delegate)协议(protocol)。另外,为什么不直接使用 UIButton?

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

我强烈推荐阅读 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)
})
}
}

关于ios - Swift Playground 错误 : Unrecognized Selector sent to instance (related to UITapGestureRecognizer),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43131395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com