 |
 |
Nib Loading Madness
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
I don't want to use NSDocument. I don't like it. I have figured out how to lead nib files and I have been able to make the objects in them release themselves when necessary but the one thing that I am unsure of is how I can create an array of pointers to the objects that are made when i load the nib files. I read http://www.omnigroup.com/mailman/arc...ly/003080.html on the Omni mailing list and am somewhat unsure of what it means. Could someone explain to me how to store the instantiated objects into an array?
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Jan 2001
Location: Virginia, US
Status:
Offline
|
|
I think NSDocument is just fine -- I'd still highly recommend it if you're really doing a multi-document app. If you just have a single window, then it doesn't make sense -- use an NSWindowController instead.
Of course, there's nothing wrong with doing it yourself either. Writing it that way a couple of times may make NSWindowController/NSDocument easier to understand as well -- it's hard for me to say.
The main idea is to use the NSBundle method +loadNibNamed:owner:, as the macosx-dev article was saying. The main thing to understand is that the "File's Owner" entry in the nib is *not* instantiated by loading the nib -- it exists before the nib is loaded. That is the "owner" argument in the above method -- the object that represents the File's Owner, which should have instance variables for any outlets needed and action methods if it's the target for anything elsewhere in the nib. All other objects in the nib are instantiated when the nib is loaded.
The general idea is to have an instance of some controller class be responsible for loading the nib, and represent the File's Owner by passing "self" as the owner argument. Obviously, this class should have the instance variables used for any outlets, and implement any action methods needed. Make sure the nib is only loaded once -- often done by checking to see if some outlet instance variable that could have only been set by loading the nib is non-nil. There's no problem with a single controller being the owner for separate nibs.
As for getting the list of the top-level objects in the nib yourself... ooh. The easiest way is just to have an outlet for each object you need a reference to. NSWindowController uses private API to get this list -- it would be very handy to have a low-level nib loading method that returns this array, but unless something has changed recently I don't think there is such a beast.
What would also be really cool is if Apple provided a connection inspector for NSMutableArray, letting us put an array directly in the nib with pre-set connections to a bunch of the other objects. It is possible to write one yourself, but it's not for the faint of heart.
If necessary, you can actually use a temporary NSWindowController to load the nib for you (since you can pass in an external owner to that class), and then just get its topLevelObjects array after loading the nib. I've used that once. It's a private ivar with no accessor method, but well, if you really really need it... you can call [theWindowController valueForKey:@"_topLevelObjects"] to get it.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
Okay I get it now. I was unsure about the whole file's owner thing. BTW, say I have one thing in a nib file and I want to make an arbitrary number of instances of it and I want to have a pointer to each instance of it. Well, that was kind of like the problem that I was facing but I have figured out the answer without having to make a whole bunch of nasty controller objects for all of the instances. I can have them all controlled by the object that created them.
Okay, the class that I want to control all of my objects has an instance in my main nib file, so it's created when the program begins. In the main nib file there is NOT an outlet called gameWindow. In the other nib file which has my window I have my controller class defined with only 1 outlet: gameWindow. The class has other outlets but those are all associated with the other nib file and so I didn't put them in this one. (I don't know if it's zero my variables or something when this nib file woke up.) In the second nib file I set my controller as the File's Owner and I connected the outlet gameWindow to my window. Then, in the code when I open the nib it sets the outlet gameWindow to the instance of the window that I created. I then store that in a linked list and add an observer for when the window closes so that I can take it out of the list. That's it. If I open another window using the same method, it'll open the nib the gameWindow variable will be changed from whatever it was to point to the new window and I put that into my list.
I think that's so cool. I got around having to use lost of nasty controller objects!
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status:
Offline
|
|
Oh BTW, every time that I loaded the nib it called the awakeFromNib method in my controller class. I got around problems with that by writing it like this:
- (void)awakeFromNib
{
static int a = 1;
if(a)
{
a=0;
// Do whatever nib awakening stuff here.
}
}
That was actually the first time that I every found a use for static variables.
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Feb 2001
Status:
Offline
|
|
Hey Dalgo, Thanks for linking to that macosx-dev article on loading nibs. Extremely helpful stuff.
[This message has been edited by tie (edited 04-29-2001).]
|
|
The 4 o'clock train will be a bus.
It will depart at 20 minutes to 5.
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2001
Location: Sad King Billy's Monument on Hyperion
Status:
Offline
|
|
Anyone up for a game of Mad Nibs? Get it?? Mad nibs!? Ha... Ha.
|
|
I abused my signature until she cried.
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|