协议没有解雇

时间:2017-04-02 20:14:24

标签: ios swift uibutton uitapgesturerecognizer

我的协议根本没有触发,我正在使它符合我想要触发的方法。任何帮助将不胜感激!

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

1 个答案:

答案 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)])

顺便说一句,自我是不需要的,你应该省略它。您通常需要指定self的唯一时间是在闭包中,编译器坚持要包含它以“使捕获语义显式化”。我的建议是除非编译器抱怨,否则将其遗漏。

另请注意,说“我的协议没有解雇”并没有多大意义。协议是一组商定的方法/属性。它就像一个用于在理解协议的对象之间进行通信的术语。最好说“我的委托方法没有被调用。”