views:

153

answers:

1

Hi,

I added an object to my .nib and I connected IBOutlets to it. But the object doesn't initiate the .nib. Another object does that. When I initiate the object added to my .nib (from somewhere in my code), then all IBOutlets are nil, even if the nib has been loaded.

What am I doing wrong here.

A: 

You don't need to do that. If you initiate an instance of the same class as the object you added to the nib, you now have 2 of them. The instance that you initiated doesn't have any IBOutlets connected. The instance in the nib has connected outlets.

Make sense?

mustISignUp
Makes perfectly sense. But how would I access the IBOutlets without having to load the window? (return nil if not loaded). I mean initiating the object from within my code, not from within the .nib.
Jonas
You dont, the object in the nib is fully initiated and outlets are hooked up when you load the nib. If you wanted to make another instance, hooked up the same way, without loading the nib, you need to hook up each outlet manually, by code. ie foo.controller = self; foo.window = self.window. etc.
mustISignUp
Okay, I need to hook them up. But I don't understand where foo refers to. Can you help me just one more time, please?
Jonas
Sure, no problem. Let's take a step back. The nib is a collection of objects saved to disk that you can unarchive when you need them. Usually they are objects like windows, views, buttons and tables, because GUI objects are just EASIER to create visually in Interface Builder. When you load the nib these objects exist, but how do you access them from your regular code? That's what the IBOutlets are for.. you have variables like IBOutlet NSVew *fooView in the class that loads the nib, and using Interface Builder you hook it up to the view you want to access. cont..
mustISignUp
.. so you have your variable BOutlet NSVew *fooView but you don't want to hook it up to the object in the nib - you want to hook it up to an object you have manually, so fooView = [[NSView alloc] init]. Make sense?What i don't get is.. you have added an object to your nib - why dont you want to use it? Why do you want to make another?
mustISignUp
Hmm, probably because I'm a terrible code designer. I removed the object from the nib. My goal is to init objects from in my code and access interface elements from within it if they're loaded. If they're not loaded then I'm happy with nil and I'll go with the defaults. So I actually think I don't need fooView = [[NSView alloc] init]... I have no idea what I do need to make it happen..
Jonas
Sounds like you are on the right track, keep your nib(s) as your Views (as in model view controller). Your Model will be your regular code, or maybe Core Data. Then finally you should have Controller Classes, responsible for loading the nibs and where you put the IBOutlets
mustISignUp
In the NSWindowController, I load the nib and have the IBOutlets. But then I won't be able to see if the outlets are nil before allocating/initiating the NSWindowController right?
Jonas
The window controller loads the nib, right? If the nib isn't loaded then the objects are just some xml on disk and the outlets are pointers to nil. When you load the nib (ie parse the xml) the objects are created and the IBOutlets are hooked up to the created objects (if you set that up to be so in Interface Builder). If the window controller loads the nib, but you haven't made an instance of the window controller yet - then there aren't even any outlets yet - they aren't nil or any other value - and there certainly aren't any 'loaded from the nib' objects for them to point to.
mustISignUp
I think I got it now. Thank you. I'll create a static singleton which points to the NSWindowController. If it's nil, then I'll go with the defaults, else I'll use the singleton's IBOutlets. Haven't tested this but I don't see why it won't work.
Jonas