我一直收到以下错误:
由于未捕获的异常终止应用程序' NSInvalidArgumentException',原因:' - [UILabel theUserClicked:]:无法识别的选择器发送到实例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
}
})
}
}
答案 0 :(得分:1)
UILabel没有匹配theUserClicked
的方法声明。
相反,将方法theUserClicked
移入NotesCardView并将self传递给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)
})
}
}
答案 1 :(得分:0)
您的点按手势识别器为NoteCardView
,其类为UILabel。因此theUserClicked:
行动将发送至NoteCardView
。要解决此问题,请将目标设置为MainViewController
实例。或者将该动作函数移动到可用的类,并使目标成为该类的实例。