问题
I'm a complete beginner in Objective-C and Cocoa.
I would like to create a window-less application, which just shows a NSStatusItem
in the system tray. The tray works fine, however, there is one problem.
For some reason my application automatically creates a window for me, which I do not want.
I thought it was caused by the automatic Interface Builder template created when I created the application in Xcode, so I deleted the .nib file from the project. However the window still gets created.
The only lines that contain a reference to the window are in the header:
NSWindow *window;
@property (assign) IBOutlet NSWindow *window;
and in the implementation file:
@synthesize window;
Both were added automatically, I did not write this.
How do I just stop the app from creating a window? I even tried to removing all references to window
from the code, including the NSWindow *window
, but the window still got created.
My temporary fix right now is to call [window close];
in the application, but surely there is a better way?
回答1:
My suspicion is that nothing in your code is creating the window. When you create a new Cocoa Xcode application, Xcode sets up an XIB with your interface for you. Open up MainMenu.xib (should be under Resources) in interface builder and delete the window that it throws in by default.
回答2:
If you don't want to show a window you may consider run your application in background. That will stop the window to appear.
In order to run your application in the background, set YES
to "Application is background only" in your app's PLIST file
回答3:
Return false in your NSDocument subclass' windowNibName method.
来源:https://stackoverflow.com/questions/6784182/window-less-cocoa-application