我的协议根本没有触发,我正在使它符合我想要触发的方法。任何帮助将不胜感激!
public class NoteCardView:UIView {
@IBInspectable var delegate: NoteCardViewDelegate?
func handleTitleLabelTap(_ recognizer:UITapGestureRecognizer) {
// Verified this function is being called and has the right value!
self.delegate?.noteCardViewTitleLabelDidRecieveTap(MainViewController.NoteCardArray[Int(recognizer.view!.tag)])
}
}
public protocol NoteCardViewDelegate {
// This should be being fired..
func noteCardViewTitleLabelDidRecieveTap(_ view: NoteCardView!)
}
extension MainViewController: NoteCardViewDelegate {
// this is what the handleTitleLabelTap function should do!
func noteCardViewTitleLabelDidRecieveTap(_ view: NoteCardView!) {
print("6")
let card:NoteCardView = (view)
UIView.animate(withDuration: 0.5, delay: 0.0, options: UIViewAnimationOptions.curveEaseInOut, animations: {
card.contentView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: (self.elementPlacement / 4) + 20)
card.contentView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 100)
}, completion: { (finished: Bool) in
self.elementPlacement = (self.elementPlacement + 1)
card.titleLabelTapGestureRecognizer!.isEnabled = !(self.elementPlacement == 5)
})
}
}
答案 0 :(得分:0)
这一行:
self.delegate?.noteCardViewTitleLabelDidRecieveTap(MainViewController.NoteCardArray[Int(recognizer.view!.tag)])
...如果self.delegate为零,则不会执行任何操作。 “?”告诉编译器检查nil并在 为
时停止。我建议添加一个打印声明:
print("About to call delegate. delegate = \(delegate)")
delegate?.noteCardViewTitleLabelDidRecieveTap(MainViewController.NoteCardArray[Int(recognizer.view!.tag)])
另请注意,说“我的协议没有解雇”并没有多大意义。协议是一组商定的方法/属性。它就像一个用于在理解协议的对象之间进行通信的术语。最好说“我的委托方法没有被调用。”