如何关闭OSX窗口?

如何关闭OSX窗口?

问题描述:

我想创建一个 主要 仅涉及状态栏的应用程序.到目前为止,我已经使用NSMenuNSStatusBar创建了状态栏项目,并且还删除了带有以下加载代码的停靠图标:

I wanted to create an application that mainly only involves the status bar. So far I have created the status bar item using NSMenu and NSStatusBar and I have also removed the dock icon with this bit of code on load:

[NSApp setActivationPolicy: NSApplicationActivationPolicyAccessory];

但是我仍然拥有的是打开应用程序时出现的NSWindow.

But what I still have is the NSWindow appearing when opening the app.

如何防止这种情况?我也希望能够重新打开它.

How can I prevent this? I also would like to be able to re-open it.

我想出了一种可怕的方法来关闭它:

I have come up with a horrible way to close it:

[_mainView setFrame:CGRectMake(0, 0, 0, 0)];

_mainView是视图控制器中主要的NSView,与nswindow相连.

Where _mainView is the main NSView in my viewcontroller that is conected to the nswindow.

然后,我希望能够再次打开该窗口,但这次需要一个桌子.但是我得到了错误:

I then want to be able to open the window again but with this time a table. But I get the error:

<Warning>: void CGSUpdateManager::log() const: conn 0x18de3 token 0x31fffffffffdafd

将框架重置为原始尺寸时.

When resetting the frame back to the original size.

此外,如果我关闭NSView,则意味着我无法再次重新打开视图.

Also if I close the NSView that then means I can't re-open the view again.

这是一个可以控制视图控制器所在的窗口处于打开,关闭状态的应用程序的冗长解释.

This is a long winded explanation of an application that can control whether the window the viewcontroller is in, is opened or close.

制作一个NSStatusBar项目应用程序,该应用程序仅显示在状态栏中,而不显示在Dock或应用程序"选项卡中.并且不显示任何正常菜单.即文件,编辑,查看等.

To make a NSStatusBar item app that only shows in the status bar and not in the Dock or Application Tabbing. And not show any of the normal menus. i.e file,edit,view and so on..

您需要添加

You need to add the Application is agent (UIElement) - (Boolean) YES key - value to the application info.plist.

还要确保在属性检查器中关闭了启动时可见"窗口.

And also make sure that the windows 'visible At Launch' is switch off in the attribute inspector.

更新:

在无故事板应用程序(OS X)中

In a none storyboard application (OS X)

在IB中将窗口的启动时可见"设置为关闭,将停止该窗口在启动时出现.

Setting the 'visible At Launch' to off in IB for a window, will stop the window appearing at launch.

但使用情节提要应用程序.这是行不通的.

But with a storyboard application. This will not work.

启动时可见"已设置为关闭.但是无论如何,该窗口将始终显示.

The 'visible At Launch' is already set to be off. But regardless of that, the window will always show.

(我认为这是苹果设计的故事板和人机界面指南的一部分.可能是因为它们源于iOS,并且应该始终有一个窗口.)

(I think this is part of the design of storyboards and Human interface guidelines by apple. Maybe because they stem from iOS and there should always be a window present.)

可能有几种方法可以更改此行为,但是我发现,如果您取消选中NSWindowController的属性检查器中的初始控制器,则

There are possibly a few ways of changing this behaviour but I found that if you uncheck the initial Controller in the Attributes Inspector for the NSWindowController

这将停止启动时显示的窗口.这是有道理的,因为该应用程序现在没有任何指令可以显示任何初始内容.

This will stop the window showing up at launch. Which makes sense since the app now does not have any instructions to show anything initially.

要打开窗口,您只需将菜单项链接到IB中NSWindowController的 Presenting Segue Show:方法.

To open the window you can simply link a menu item to the NSWindowController's Presenting Segue Show: method in IB.

如果要以编程方式打开窗口,则必须在代码中重新指向控制器.

If you want to open the window programmatically, then you have to re point to the controller in the code.

  • 在IB中,再次选择NSWindowController
  • 转到身份检查器.
  • 为情节提要ID提供"Main"身份

  • 现在转到您的 AppDelegate.h 文件,并添加一个Property和IBAction:

  • Now go to your AppDelegate.h file and add a the Property and IBAction:

         @property (strong) IBOutlet NSWindowController *winController;

          -(IBAction)showWindow:(id)sender;

  • 然后转到 AppDelegate.m 文件,并将此代码添加到 applicationDidFinishLaunching

  • Then go to the AppDelegate.m file and add this code in the applicationDidFinishLaunching

             NSStoryboard *storyBoard = [NSStoryboard storyboardWithName:@"Main" bundle:nil];
    
             NSWindowController * main = [storyBoard instantiateControllerWithIdentifier:@"Main"];
    
             _winController = main;
    

  • (请注意,仅通过直接将其与AppDelegate中的属性链接来添加控制器对我不起作用)

    (note just adding the controller by linking it directly with a property in the AppDelegate did not work for me)

    • 现在将IBAction代码添加到 AppDelegate.m

             -(IBAction)showWindow:(id)sender {
    
              [_winController showWindow:self];
    
    
             }
    

    您将需要正常将IBAction链接到您要通过IB打开窗口的任何菜单项.

    You will need to link the IBAction as normal to which ever menu item you want to open the window via IB.