3
votes

I've created a new cocoa application using .xib files (not storyboarded, the app has to be backwards compatible with mavericks/mountain lion) and I would like to have a custom windowcontroller for the main window. Is this possible? I can't seem to find a way to connect the window to my desired custom controller. The window has a reference outlet in AppDelegate, however I need a custom NSWindowController for this window as it doesn't open on application launch. The application launches silently as a menu bar app and the main application is launched via button press in the drop down from the menu bar.

Is there a way to link a controller to the window in the interface builder? Or do I have to do something along the lines of :

 wc = [[CustomWindowController alloc] initWithWindowNibName:@"Main"];

Thanks!

3

3 Answers

3
votes

Yes, open up Utilities (the right panel) in Interface Builder, and at the bottom click on the Object Library (circle with square in it).
Search for Object (blue cube), and drag it into your Document Outline (the panel on the left inside of interface builder)
From there, select that object you just created, and change the Class in the Identity Inspector to be the window controller you want.

Finally you can go into the Connections Inspector and connect your window to the window outlet

3
votes

I can't seem to find a way to connect the window to my desired custom controller. The window has a reference outlet in AppDelegate, however I need a custom NSWindowController for this window as it doesn't open on application launch.

Another way:

1) Delete the window in MainMenu.xib. Delete the window property in AppDelegate.m--because you deleted the window, it is no longer relevant.

2) File>New>File>Cocoa Class. Enter a class name, e.g. MainWindowController; select "Subclass of: NSWindowController"; check "Also create .xib file for user interface".

3) Create an outlet in AppDelegate.m:

#import "AppDelegate.h"
#import "MainWindowController.h"

@interface AppDelegate ()

@property (strong, nonatomic) MainWindowController* windowController;

@end

4) Declare an action in AppDelegate.h:

@interface AppDelegate : NSObject <NSApplicationDelegate>

-(IBAction)launchWindow:(id)sender;

@end

and implement it in AppDelegate.m:

- (void)launchWindow:(id)sender {
    [self setWindowController:[[MainWindowController alloc]
                               initWithWindowNibName:@"MainWindowController"]];

    [[self windowController] showWindow:nil];
}

5) In MainMenu.xib, hook up the Menu Item to the launchWindow() action: control drag from the Menu Item to the AppDelegate object and select launchWindow.

0
votes

Create the controller and make it extend from NSWindowController. In your xib file select the File's Owner and set it to your custom class. Select your NSWindow and connect it to the File's Owner.

To open the window:

In your .h:

@property (strong, nonatomic) YourWindowController *yourWinController;

In your .m:

self.yourWinController = [[YourWindowController alloc] initWithWindowNibName:@"YourWindowController"];
[self.yourWinController showWindow: nil];