2
votes

I have a simple Cocoa app. It has two windows, each in a separate xib file:

  • MainMenu.xib
  • SecondaryWindow.xib

I have an AppDelegate class, which has a reference to the window in MainMenu.xib. I'm trying to make it have a reference to the window in SecondaryWindow.xib. I'm confused about how to do this. I have made an outlet, as such:

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSWindow *secondaryWindow;

@end

Here's the implementation:

@implementation AppDelegate

@synthesize window = _window;
@synthesize secondaryWindow = _secondaryWindow;


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [NSBundle loadNibNamed:@"SecondaryWindow" owner:self];
    NSLog(@"_window = %@", _window);
    NSLog(@"_secondaryWindow = %@", _secondaryWindow);
}

@end

_secondaryWindow is always (null)

I've add an outlet from in SecondaryWindow.xib connection the second window to the outlet in AppDelegate. What else do I need to do in SecondaryWindow.xib to make the connection complete?

EDIT: added [NSBundle loadNibNamed...]

2

2 Answers

1
votes

You need to load it... By default, MainWindow.xib is loaded by the framework, which creates its own instance of the app delegate. You should load your second window from your app delegate (try [NSBundle laodNibNamed:@"SecondaryWindow" owner:self]. when you do this, the File's owner will be the application delegate - change the class of the file's owner in interface builder to reflect that and make your connections to it)

1
votes

Did you set the type of File's Owner in the secondary window's .xib to the type of your app delegate? And did you then connect the window in that .xib to the secondaryWindow outlet of File's Owner?

If you did those things, and if the .xib is properly included in the project and you've specified the name of the file correctly in the +loadNibNamed:owner: message, then your secondaryWindow property should be populated.