macOS - I'm subclassing AVPlayerView to create my own keyDown functionality... I need to get the keyDown data from my AVPlayerView subclass to my viewController. Using delegate / protocol (although seriously thinking of abandoning it for NSNotification or something else). Here's what I have so far:
This follows the pattern I have seen in many internet delegate / protocol tutorials. I suspect that perhaps when I'm saying "var customPlayerView: CustomAVPlayerView?" that I'm instantiating ANOTHER PlayerView, not the one I already have. ??? Or, there is something about it being optional? For example, if I change the var to let, I get an error saying I have no initializers. I have tried creating initializers, and I'm just LOST. I don't even know what I'm trying to initialize... the delegate? It leads down this path of "required inits" and super.inits and it's just an unholy mess. Super frustrating. Guidance?
Code Block class ViewController: NSViewController, ReceiveKeyDownData { var customPlayerView: CustomAVPlayerView? override func viewDidLoad() { super.viewDidLoad() customPlayerView?.keyboardDelegate = self } func receiveKeyboardData(data: UInt16) { print("from VC: \(data)") //this does not work! } } protocol ReceiveKeyDownData: class { func receiveKeyboardData(data: UInt16) } class CustomAVPlayerView : AVPlayerView { weak var keyboardDelegate: ReceiveKeyDownData? override var acceptsFirstResponder: Bool { get { return true } } override func keyDown(with event: NSEvent) { if self.superview != nil { print("from PlayerView: \(event.keyCode)") // this works! self.keyboardDelegate?.receiveKeyboardData(data: event.keyCode) } } }
This follows the pattern I have seen in many internet delegate / protocol tutorials. I suspect that perhaps when I'm saying "var customPlayerView: CustomAVPlayerView?" that I'm instantiating ANOTHER PlayerView, not the one I already have. ??? Or, there is something about it being optional? For example, if I change the var to let, I get an error saying I have no initializers. I have tried creating initializers, and I'm just LOST. I don't even know what I'm trying to initialize... the delegate? It leads down this path of "required inits" and super.inits and it's just an unholy mess. Super frustrating. Guidance?