# Loading Auxiliary Nib files



## aridner (Aug 1, 2001)

Hi all, 

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. 

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)showPreferencesid)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 ran 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 

- Take a little, give a little - Hi all,


----------



## rharder (Aug 1, 2001)

I've never tried working with multiple windows in that fashion--only documents, and even then I've only dabbled.

I seem to recall that I used the openUntitledDocumentOfType:display: method of an NSDocumentConroller instance to open a new window, but it sounds like  it's not documents you're opening, it's other kinds of utility/prefs/etc panels.

I can only confirm that I also could not quite figure out how to make other windows load up dynamically.

If you haven't already, maybe check out some sample code from the OmniGroup's AppKit ( http://www.omnigroup.com ) to see if they do something like what you're doing. Maybe check the OpenUp package ( http://www.stepwise.com/Software/OpenUp ) too.

Good luck.

-Rob


----------

