0

I'm trying to add a menu item to show the main window. It's enabled when the window has not been minimized or closed (yellow - and red x buttons), but when it has, the menu item is grayed out. That is of course useless and silly. :)

Another menu item, File/Page Setup... is NOT grayed out in the same way (on minimize/close). I have found no differences in IB between this menu item and the one I created, except the title. Both are connected to First Responder selectors (runPageLayout: for page setup, and showWindow: for mine), both have keyboard shortcuts, and both are enabled.

Likewise, the parent menu items are identical, except for the title.

How do I make sure this Show Main Window menu item is available when the user has pressed the red x or the yellow - button?

Edit: I made the menu items not gray out by unchecking Auto Enables Items for the parent menu item.

That made this method in the standard NSDocument class for the application be called:

- (IBAction)clickMinimizeButton:(id)sender {
    NSWindow *ww=[[NSApplication sharedApplication] mainWindow];
    NSLog(@"%@",ww);
}

And ww is a valid object.

The problem is, I can't restore the window from minimized/closed, because the method isn't called when it is.

How do I make this method be called when the window is minimized/closed? I want to simply do the opposite of the minimize or close click.

There's only one window in the app, and clicking the dock icon again does show the window, but Apple requires a menu item to do the exact same thing.

It would seem I have to connect the menu item to a custom, created by me, application method (certainly no application method restores all windows, for example), but I don't know where to declare it, since I'm not as experienced with Cocoa as I am with Cocoa Touch.

Edit 2: Going from the above, I created a method in the application delegate and connected it to the menu item. This gets the method called even when minimized, but I don't know the correct code to restore the main window. This desperate try of course only activates the app that is already active, not the window that is minimized.

NSApplication *theapp=[NSApplication sharedApplication];
[theapp activateIgnoringOtherApps:YES];

So this is all that is left now. How to emulate the standard "Bring All to Front" menu item in a default application? I will have a look at some standard windowed Mac app from Apple.

Edit 3: I created a standard Cocoa Application, and it too grays out all menu items in the Window menu when the window is minimized. But the default NSDocument is automatically added below these, and is not grayed out. I will be happy with such a solution.

For some reason, in my Window menu, my NSDocument does not automatically appear. The Info.plists are identical (apart from pointing to differently named NSDocument classes, of course).

How do I

a) programmatically add the document to that menu,

b) get the Window menu to automatically add it as it does in the standard application, or

c) replace my "damaged" Window menu (if that is the case) with a Window menu from my standard app that behaves as expected?

4

2 回答 2

0

尝试将您的方法重命名为其他名称。 showWindow:已经在 NSWindowController 中声明,这可能会产生冲突。还要确保没有释放应该响应菜单项的对象。

于 2012-10-18T08:39:11.870 回答
0

你实现了你的showWindow吗?

- (void)showWindow {
    if (!self.window) {
        [NSBundle loadNibNamed:@"MainWin" owner:self];
    }

    [self.window makeKeyAndOrderFront:self];
}

ps你window不能autoreleased关闭窗口(Behavior-> Release When Closed

于 2012-10-18T10:05:31.863 回答