Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a custom NSWindowController on launch?

In this answer is said it is possible to have a custom NSWindowController by removing the window from MainMenu.xib and instantiating the window controller 'manually' from applicationDidFinishLaunching:. But it also says:

To stop the default window from showing I just delete it. There's probably a better way but I don't know that.

Is there a better way? What is that better way, should it exist? Is it considered 'normal' practice to get your own window controller?

like image 236
11684 Avatar asked Feb 07 '13 17:02

11684


2 Answers

To do this, you would usually subclass NSWindowController and change the File's Owner class to your WindowController subclass in the nib.

EDIT:

If you aren't doing a document-based app, and just want an NSWindowController of your own to do on-demand loading of Nibs (completely reasonable), then you'd delete the window from your nib and instantiate an NSWindowController subclass programmatically, using it explicitly to do your window loading...

@implementation MyApplicationDelegate {
    MyWindowControllerSubclass *windowController;
}


-(void)applicationDidFinishLaunching:(NSNotification *)notification {
    windowController = [[MyWindowControllerSubclass alloc] initWithWindowNibName:@"MyWindowNib"];

    [windowController showWindow:nil];
    [windowController.window makeKeyAndOrderFront:nil];
}
like image 64
iluvcapra Avatar answered Nov 10 '22 16:11

iluvcapra


I was running into the same issue and I want to show you my own solution.

  1. Create a normal Cocoa Application (not Document Based)
  2. Go to MainMenu.xib an delete the Window
  3. Go ahead and create a new file, User Interface -> Window
  4. After that create a subclass of NSWindowController
  5. Open the just created xib file and set the Custom Class in the Identity inspector to the just created subclass of NSWindowController
  6. Right click on File's Owner and connect the window property to the actual window
  7. Now go to the AppDelegate an create an instance variable that holds you CustomWindowController
  8. Last thing you have to do is instantiate your CustomWindowController self.customWindowController = [[AccountWindowController alloc] initWithWindowNibName:@"CustomWindow"]; and show the Window [self.customWindowController showWindow:nil] in - (void)applicationDidFinishLaunching:(NSNotification *)aNotification

Here is an example project https://www.dropbox.com/s/ft3t7w72806tnoe/CustomWindowController.zip

like image 7
tuna Avatar answered Nov 10 '22 16:11

tuna