Oortone
Oortone

Reputation: 185

Where is my closed NSWindow?

I have an Application delegate that holds the reference to a NSWindow and the program (small test program so far) is generally working with Main Menu and views in the window.

However I discovered that if I close the window holding the views it's gone although the program is still running. As far as i can see the window reference is not nil but how do I restore it so it's visible and shown under the Windows menu again?

The program is not document based. All actions are performed in the window in question.

I created the window in the MainMenu.xib that was auto created by Xcode (this was in Xcode7 or 8 but now I've upgraded to 9).

I'm new to windows handling on Mac so I understand this is a very basic question but I'm totally stuck here. Having a window that is supposed to hold all functionality disappear without the user being able to restore it is bad I believe.

Upvotes: 1

Views: 426

Answers (1)

pfandrade
pfandrade

Reputation: 2419

If you have a reference to the NSWindow, you just need to show it. For example:

[window makeKeyAndOrderFront:self];

But if the user closed the single window of your app, than the question is: How will the user trigger that code? The simplest answer is that the user will click your app's icon on the dock. To handle that click, implement the following method on you app delegate.

- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)hasVisibleWindows
{
     if (hasVisibleWindows) {
         return YES;
     }
     [window makeKeyAndOrderFront:self];
     return NO;
}

Upvotes: 4

Related Questions