Help Loading Auxiliary Nibs files
Help Loading Auxiliary Nibs files
- Subject: Help Loading Auxiliary Nibs files
- From: Adrian Ridner <email@hidden>
- Date: Wed, 1 Aug 2001 11:03:47 -0700
Hi all,
I'm new to the list, but I hope and can find some fellow Cocoa
developers with any ideas...
I'm trying to use lazy loading of nib files when a user click at the
menu item in a Cocoa application. After looking at several examples (I'm
new to Objective-C although with solid C/C++ background), I decided that
some panels like the Preferences only require shared instances. I split
all my windows (about 9 created from clicking menu items) in different
nib files and tried to share instances. I seem to be able to load the
files (all the methods are called) including awakeFromNib....and
showWindow, however no window displays. Only the main window loads. I
have tried different ways of loading nibs, and nothing worked. What is
the standard practice for loading other nibs besides MainMenu? I have
looked at endless tutorials including "Learning Cocoa" (no help) and
Cocoa Dev Central, etc. All help is appreciated. It is actually worth
mentioning I'm not dealing with Documents (my app just have panels like
Preferences). How do you load your Preferences nib?
Here is how I setup my code just in case it helps someone. Let me know
if you see something wrong:
I have my AppDelegate where all the actions from the menu's are
connected to, then I have the auxiliary nib (say Preferences.nib), and
a frozen object in the nib with all the connections and a controller in
my application that owns the nib.
I derive all my controllers from a common object subclass of
NSWindowController that has this code in init (and I keep the naming of
the controller object the same as the nib file:)
- (id) init {
// Continue the designated initializer chain:
[super init];
// here's a fuller invocation of "loadNibNamed:" which shows the
loading of the
// dictionary with the key-value pair NSOwner, which has a value of
"self".
[NSBundle loadNibFile:[[NSBundle mainBundle]
pathForResource:NSStringFromClass([self class])
ofType:@"nib"]
externalNameTable:[NSDictionary
dictionaryWithObjectsAndKeys:self, @"NSOwner", nil]
withZone:[self zone]];
// place other initialization code here
return self;
}
Then the Preferences.m, the controller for Preferences.nib, inherits the
init above and has the shareInstance method below:
+ (id) sharedInstance {
static Preferences *_shared = nil;
if (!_shared) {
NSLog(@"This means the instance was not created yet");
_shared = [[Preferences allocWithZone:[self zone]] init];
}
return _shared;
}
Then the frozen object (PreferencesCO.m) has the usuals:
IBOutlets/actions and some reference code just to know they are being
called:
- (id) init {
[super init];
NSLog(@"In init of PreferencesCO");
return self;
}
- (void) awakeFromNib {
NSLog(@"in awakeFromNib in PreferencesCO");
}
Finally when a user clicks Preferences... in the menu the corresponding
IBAction is called in the application delegate and this code runs:
- (IBAction)showPreferences:(id)sender {
NSLog(@"I'm in Preferences...");
[[Preferences sharedInstance] showWindow:sender];
NSLog(@"Just loaded the Preferences in AppdDelegate");
}
All input and ideas are more than welcome, I have run out of ideas and I
can't move forward on this. Please help and feel free to use the ideas
here for your own...
Adrian Ridner
****************************
- Take a little, give a little -