JonF
JonF

Reputation: 2436

Reopen my application window from menu selection in status bar

When the red X is pressed my window goes away but, as designed, my status bar item stays in the status bar.

When you click on the status bar item it opens a menu. One of the choices is to reopen the application. It is able to call an action in the app controller, however I'm not sure what to do to open my application's window back up. I read that

[window makeKeyOrderFront:self];

Would accomplish this but "window" isn't recognized by the compiler. I'm new to objective c/cocoa so I'm sure I'm missing something obvious.

Upvotes: 1

Views: 1189

Answers (2)

DenVog
DenVog

Reputation: 4286

I recently tacked this, and here's what worked for me. All three things are handled in AppDelegate:

// Response to reopen application menu selection

- (IBAction)showMainWindow:(id)sender
{
    [self applicationShouldHandleReopen:nil hasVisibleWindows:YES];
    [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}

// Need this if you want the MainWindow to reappear after the user has closed it

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [_window setReleasedWhenClosed:NO]; 
}

// Need this if you want the MainWindow to reappear after the user has closed it

- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag{
    [_window setIsVisible:YES];
    return YES;
}

Upvotes: 0

Simon Urbanek
Simon Urbanek

Reputation: 13932

Here window is the variable you used to store the NSWindow* object from your application - it assumes that you still are still holding it in one of your classes (typically in the app delegate - the default Xcode app delegate template even creates a property for window). However, all this depends on the type of your application -- this should be all automatic if your application is document-based (you can call openUntitledDocumentAndDisplay:error: to create a new document). If it's not then it is really entirely up to your code to manage the window - typically in the app delegate.

Upvotes: 1

Related Questions