Tristan Warner-Smith
Tristan Warner-Smith

Reputation: 9771

How does window management work in Mac development for MenuBar apps?

I'll preface this by saying this is my first Mac application, though I've been building iOs apps for some time.

I've got a menu bar app (system tray app) by which I mean I've got this.

I now want to show an NSWindow I've created in Interface Builder so I've created a class that's derived from NSWindow. Made my class the Window's delegate and from the App Delegate I do this:

MyClass *myClass = [[MyClass alloc] init];
[myClass display];
[myClass center];
[NSApp activateIgnoringOtherApps:YES];
[viewer makeKeyAndOrderFront:nil];

This seems to show a window without the standard window buttons (minimise, maximise, close) rather than the window I've defined.

Is this the right way to be showing windows? and how should the Window be defined so it shows my designed interface?

In Windows Forms programming this would be:

Form myForm = new Form();
myForm.Show();

Upvotes: 0

Views: 577

Answers (1)

sbooth
sbooth

Reputation: 16976

You rarely need to subclass NSWindow unless you want to override a window's behavior. A more typical usage scenario would be to use an instance of NSWindowController or a subclass of NSWindowController to manage the window, by making that class the File's Owner in Interface Builder. Once this is done, to grab an instance of the window use:

NSWindowController *wc = [[NSWindowController alloc] initWithWindowNibName:@"NIBNAMEHERE"];
[wc showWindow:nil];

Another alternative in your app would be to add an NSWindow IBOutlet to your app delegate, and load the window in your app delegate with:

[NSBundle loadNibNamed:@"NIBFILENAME" owner:self];
[_window makeKeyAndOrderFront];

Upvotes: 2

Related Questions