Wondering whats the best approach to implement delegates for Cocoa controls, classes. Any recommendations or examples will be greatly appreciated…
Im doing the basic calling Obj-C to create a class for a delegate (with/out protocols) and creating corresponding instances, all inspired by macoslib, ioskit and other projects found online.
Is it possible to uses instance methods as callbacks? (i only got it working with shared methods, when using instance methods the “ID” received was not the same (Ptr) of the delegate class instance I created in code)
I ended up with a “delegateClass” wrapper that I subclass, with a dictionary to keep track of classes instances already created and the actual delegate instances with its corresponding weakRef pointing to its owner.
I was looking on how to clean this dictionary, I notice that by the time I get the control “close” event or the destructor is invoked my instance Ptr is already null. I ended up running a ForEach every so often on the lifeCycle of delegateClass to look for Nil weakRef and removing them from the dictionary.
For each delegate a control implements a separate class instance of “delegateClass”, in general (most use-cases) in the callback (shared method) I end up calling a handler/method on the owner (weakref to control, canvas, etc). I tried implementing this using the actual control class (similar to how macoslib does it) with some helper functions to reduce the scaffolding but it was still quite a lot of work to repeat on each control/class (a bit ugly, the separate class made it cleaner). Is there anything else more elegant/reusable?