views:

300

answers:

3

This is kind of a followup to this question. I have the following code:

- (BOOL)applicationShouldHandleReopen:(NSApplication *)app hasVisibleWindows:(BOOL)flag {
    NSLog(@"Has visible windows: %d, main window visible: %d", flag, [[app mainWindow] isVisible]);
    if (!flag) {
        // I need to make mainWindow visible again here. HOW???
        // I tried the following but it has no effect.
        // [[app mainWindow] makeKeyAndOrderFront:self];
        return NO;
    } else {
        return YES;
    }
}

There is [window isVisible] message that I can use to query, but no corresponding setVisible or "show"...

+1  A: 
[[app mainWindow] makeKeyAndOrderFront:self]
nall
+2  A: 

Assuming you actually have a main window in existence and it's just ordered out for some reason, use makeKeyAndOrderFront:. I would bet, though, that you don't have a main window to talk to, so you'll want to either bring up some window you have a reference to or open a new window.

Chuck
This seems to have no effect and no window comes up (i.e once I close main window, can't get it back.) I don't know how to tell if the main window is still in existence or not.
Jaanus
+5  A: 

First, “main window” in Cocoa doesn't mean what I think you think it means. The “main window” is the window that is active. If you don't have any windows ordered in, then no window is the main window, and mainWindow will return nil; if a window is active, it's the main window, but it's not a window that's ordered out (if it were, it couldn't be active).

So, you need to create an outlet to the window that you consider the main window.

isVisible will tell you whether that window is ordered in. If it's ordered out (isVisible returns NO), you'll want to order it in.

Of course, you can't just order it in because that wouldn't specify what order you want it to have. So, you want to order it front.

But making it the frontmost window doesn't count for much if it doesn't have user focus—that is, if it isn't key. So, you want to make it key and order it front.

The message you'll send it to do that is makeKeyAndOrderFront:. Making it key will also make it main.

Peter Hosey
Thanks, this is helpful. Yes, looks like I was expecting a bit more from "main window" and thought it is more special.I think I am confused because I am looking at my NIB file and I see Application and Window side by side. But apparently they have different lifecycle policies and Window is not really a persistent object.So let me ask this — if I want to have just one window through my app, what is the most appropriate thing to do? Should I retain the object myself somehow in the controller? Should I reload it from NIB every time applicationShouldHandleReopen is invoked? Or?
Jaanus
Yes, applications and windows are not the same thing in Mac OS X, and your application has no top-level window containing all the others. And yes, you retain the window as long as you want it to exist. When you want it to stop existing, close it and (if the relevant box in IB is unchecked) release it. It is possible, and sometimes desirable, to load the same nib more than once—but this will give you a new copy of the objects in the nib every time, which is not what you want in this case.
Peter Hosey
thanks... as a followup, I finally got it working as I wanted by making my main app controller a subclass of NSWindowController and pointing its "window" outlet to the desired window. This way the window is persistent.
Jaanus