Re: Encoding custom class from Interface Builder?
Re: Encoding custom class from Interface Builder?
- Subject: Re: Encoding custom class from Interface Builder?
- From: Ricky Sharp <email@hidden>
- Date: Thu, 05 Jul 2007 05:29:44 -0700
On Thursday, July 05, 2007, at 06:56AM, "Stephane Sudre" <email@hidden> wrote:
>
>On 5 juil. 07, at 8:23, Peter Duniho wrote:
>
>> Please bear with me, I'm new to Cocoa, and it's been almost 15 years
>> since I did any Mac development.
>>
>> General scenario: I created a simple interface in IB, with a window
>> and a custom view class, derived from NSView.
>>
>> In that custom view class, I want to do some initialization. However,
>> overriding the NSView initWithRect method doesn't work. I understand
>> this is because the class was instantiated in IB, rather than at
>> runtime, archived and then unarchived later at runtime.
>>
>> The suggestion I found in the past messages on this mailing list is to
>> override the awakeFromNib method, and do initialization there. That
>> works, but now I've got initialization code called from two different
>> places: initWithRect and awakeFromNib.
>>
>> That somehow seems ungraceful to me, and it occurred to me that if I
>> could just introduce my custom class members into the archive, then I
>> could have my data unarchived along with the base class stuff.
>> However, I'm having trouble getting this to work.
>>
>> In particular, as near as I can tell, even though I implement
>> encodeWithCoder and initWithCoder, only the latter gets called. It's
>> hard for me to be sure, but I get an exception trying to unarchive a
>> string instance in my class, and when I throw something extra like
>> displaying an alert into the encodeWithCoder method, it never gets
>> shown when I'm saving the nib in IB (which is where I'd expect my
>> encodeWithCoder to be called).
>>
>> Am I doing something wrong? Or is it simply a known limitation that a
>> custom class used in IB does not actually get archived when saving
>> from IB? Is awakeFromNib the only way to initialize a custom class
>> used in IB, or is there actually some approved mechanism for allowing
>> custom classes saved in the nib to be unarchived in a useful way?
>>
>> Actually, given that my custom behavior (overridden drawing, for
>> example) is not actually reflected in IB, I suppose it's possible that
>> it's not really even using my class, except to refer to it by name in
>> the nib. Frankly, that seems a bit broken to me, but it would explain
>> what I'm seeing (or not seeing, as the case may be).
>
>Are you are using this custom view multiple times in your project or
>are you planning to reuse it in the future?
>
>If the answer is No, then stick with awakeFromNib.
>
>If the answer is Yes and it's really more convenient to specify some of
>the view settings in the Nib, then it might be worth considering
>writing a Interface Builder palette.
>
>Writing an Interface Builder palette (as of today) is between simple
>and complex. It's better to start from one of the samples available in
>the /Developer folder.
>
>- initWithCoder: will be called when the view is unarchived.
>
>- encodeWithCoder: will be called only by Interface Builder (AFAIK)
>when the nib is saved.
I think encodeWithCoder is also called when first dragging the item (or its proxy) from the palette into the nib. While initially creating several palette's, I added many NSLog statements and learned quite a bit about the flow.
--
Rick Sharp
Instant Interactive(tm)
_______________________________________________
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