Pregunta

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?

¿Fue útil?

Solución

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];
}

Otros consejos

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

I actually found another way: NSWindowController has the method -initWithWindow:. Because the App Delegate has a property window which is linked to the window from MainMenu.xib on startup, it was easy to link it to my WindowController:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    MyWindowController *wincon = [[MyWindowController alloc] initWithWindow:window];
}

I have yet to research this, but I don't get any errors.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top