Can you manually implement Cocoa bindings?

后端 未结 4 1329
一向
一向 2020-12-04 08:53

I\'ve had a crack at implementing bindings for my own NSView subclass. It works, but there are problems with retain cycles when binding to File\'s Owner from a nib file. Aft

4条回答
  •  渐次进展
    2020-12-04 09:45

    The short answer is, no you can't get it to work with no workaround in the calling code and nibs. Even NSAutounbinder misses some cases for the NSDocument and NSWindowController, if Apple can't get it working correctly for 2 classes they specially rig up those of us without access to innards of AppKit have basically no chance.

    Having said that, there are two workaround that are maybe a bit nicer than unbinding in windowWillClose:.

    1. Do not bind to File's Owner, but instead to drag an NSObjectController as root level object into the nib and bind to that, then setContents: on the object controller during awakeFromNib.
    2. Turn on Garbage Collection. If that is an option it solves all the object cycle issues ;-) Obviously GC introduces its own issues, and if you need 10.4 compatibility it is a non-starter.

提交回复
热议问题