My protocol is not firing at all, and I am conforming it to the method that I want it to trigger. Any help would be well appreciated!
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)
})
}
}
This line:
self.delegate?.noteCardViewTitleLabelDidRecieveTap(MainViewController.NoteCardArray[Int(recognizer.view!.tag)])
...won't do anything if self.delegate is nil. The "?" tells the compiler to check for nil and stop if is nil.
I suggest adding a print statement:
print("About to call delegate. delegate = \(delegate)")
delegate?.noteCardViewTitleLabelDidRecieveTap(MainViewController.NoteCardArray[Int(recognizer.view!.tag)])
BTW, self is unneeded and you should omit it. The only time you usually need to specify self is in a closure, where the compiler insists that you include it in order to "make the capture semantics explicit." My advice is to leave it out unless the compiler complains.
Also note that it doesn't really make sense to say "my protocol isn't firing." A protocol is an agreed-upon set of methods/properties. It's like a lingo used to communicate between objects that understand the protocol. It would be better to say "my delegate method isn't getting invoked."