On Jan 3, 2011, at 08:27, Richard Somers wrote:
> - (void)awakeFromNib
> {
> // Create a bunch of "objects" programmatically which
> // depend on the document managed object context (which
> // is reachable at this stage of initialization).
> }
>
> @end
>
> I would like to establish bindings in Interface Builder between the "objects"
> created by the custom view and some standard Interface Builder widgets. The
> problem is 'awakeFromNib' is called AFTER the bindings are established so the
> bindings never work.
>
> Nib loading order.
>
> 1. Load contents of Nib.
> 2. Custom view receives 'initWithFrame:'.
> 3. Establish outlets, action connections, and bindings.
> 4. Custom view receives 'awakeFromNib'.
If I understand your description correctly, you actually have a bug in your
code. At the time 'awakeFromNib' is called, bindings (and therefore KVO
observations) already exist on certain properties of your custom view (that is,
on the key-paths that represent those properties) -- even though the property
values are still nil, though that's not inherently a problem.
You don't show any code from 'awakeFromNib', but your use of "create a bunch of
objects programmatically" suggests that you're creating them and assigning them
to various instance variables. Assigning to instance variables is wrong here,
because it's not KVO compliant for the corresponding properties. Of course,
therefore, existing observers of these properties don't get notified and the
bindings will malfunction in the way you've seen.
Instead of assigning the newly created objects to instance variables, or adding
them to collections held in instance variables, you should use property
setters, or KVO-compliant accessors for adding to collections.
If you *just* have instance variables in the custom view, and no formally
declared properties, then you're relying on the old-fashioned, frowned-upon
KVC-treats-instances-variables-as-properties behavior. In that case, you can
use 'will/didChangeValueForKey:' to generate the notifications, or -- far
preferable -- go ahead and really declare the properties in the proper way.
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]