Re: initWithFrame not being called on my NSImageView subclass
Re: initWithFrame not being called on my NSImageView subclass
- Subject: Re: initWithFrame not being called on my NSImageView subclass
- From: "Ken Ferry" <email@hidden>
- Date: Thu, 21 Feb 2008 11:41:17 -0800
> I'm not sure -initWithCoder: is guaranteed to be called, and is
> certainly not the "proper" place to be performing object
> initialization after awaking from a nib.
What's guaranteed is that all init methods on a class will funnel into
one of the designated init methods. A subclasser should always
override all designated initializers of the superclass (if he needs to
do work at initialization time). Then you're guaranteed to have your
setup performed no matter how the object is created.
For NSView, -initWithFrame: and -initWithCoder: are the designated
initializers.
-awakeFromNib is good if the work you're doing involves other objects
from a nib. In particular, there's a subtlety with decoding from an
archive: suppose two objects refer to each other and encode each other
(with encodeObject:forKey:) in a nib. Well, one of them has to finish
initializing before the other. The upshot is that when you call
-decodeObject:forKey: in -initWithCoder:, you'll get back an object,
but it may not have finished its initialization yet. It's fine to
send messages defined at the NSObject level (like -retain) to it, but
not to treat it as a full fledged object of whatever class you know it
to be.
In -awakeFromNib you're guaranteed that all objects in the nib have
been through initialization had have their outlets hooked up.
-Ken
Cocoa Frameworks
On Thu, Feb 21, 2008 at 11:15 AM, Kyle Sluder
<email@hidden> wrote:
> On Thu, Feb 21, 2008 at 2:06 PM, Mike Abdullah
> <email@hidden> wrote:
> > If the view's being loaded from a nib it's archived, so you need -
> > initWithCoder:
>
> I'm not sure -initWithCoder: is guaranteed to be called, and is
> certainly not the "proper" place to be performing object
> initialization after awaking from a nib. The official recommendation
> is to either create an IB plugin so that your version of
> -initWithFrame: is called at design time, or perform subclass-specific
> initialization in -awakeFromNib.
>
> http://lists.apple.com/archives/cocoa-dev/2002/Jul/msg01647.html
>
> --Kyle Sluder
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> 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:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden