PDA

View Full Version : Instantiation of objects in IB




aliher
Mar 25, 2009, 08:53 AM
Hi,

I'm trying to achieve loading of custom views from nib files.
I have a nib which contains NSView with some controls on it, custom object of class B which was loaded through classes tab and subsequently instantiated. And a filesOwner set to my custom class A. Class A contains a references to NSView and B and I bind this A's outlets in IB to appropriate objects.
Class A has a method to load itself from nib using loadNibNamed:owner: from NSBundle.
The methods returns true which means nib was loaded successfully, but when I try to look at the fields I see that reference to NSView is populated (and I actually use it inside a form to show contents), but the reference to B is always nil.
I've also tried to stick NSObjectController and add another field to A to check if it is a matter of custom class being instantiated, but the result is the same.
Only the NSView instance is created and referenced by fileOwner.
I suggest that B's instance is never created since its init method is never called according to logging and breakpoints.
I think it is quite trivial operation that I'm doing, so I should be missing something in details or conceptually.



kainjow
Mar 25, 2009, 12:50 PM
So your nib has 2 top-level objects, a view and a custom object, and the view gets created but the custom object isn't? Did you check for initWithCoder?

aliher
Mar 25, 2009, 12:57 PM
Exactly. I tried to add initWithCoder: to the custom object but it doesn't seem to change anything.
I was thinking that loadNibNamed: might be a wrong method to inflate the nib back.

kainjow
Mar 25, 2009, 01:00 PM
If you're on 10.5 I'd suggest using NSViewController as it will make your life easier.

But I'm not sure what else to say. Can you post a screenshot of your nib? Maybe it's configured incorrectly.

aliher
Mar 25, 2009, 01:03 PM
Nop I'm on 10.4 so I don't have NSViewController. And I also want to know what is actually happening under the hood. :)
I'll try to post a screenshot and a code snippet when I'm back home though.

kainjow
Mar 25, 2009, 01:07 PM
Maybe you forgot to set the class of the object and it's still an NSObject or something..

aliher
Mar 26, 2009, 04:21 AM
Problem is resolved. It turned out really a schoolboy thing.
I renamed nib file at some point in past and forgot to change the name used to load it.
Xcode kept compiled resource somewhere and it was successfully loaded, but it didn't contained appropriate objects.
Once I made clean and build it started to fail when loading nib since stale files were removed by clean.
Once I fixed the nib name in code everything started to work smoothly.
Thanks for the replies, they showed me the right way. :)