无窗口 Cocoa 应用程序

发布于 2024-11-25 16:43:46 字数 621 浏览 2 评论 0原文

我是 Objective-C 和 Cocoa 的初学者。

我想创建一个无窗口应用程序,它只在系统托盘中显示一个 NSStatusItem 。托盘工作正常,但是有一个问题。

由于某种原因,我的应用程序自动为我创建一个窗口,这是我不想要的。

我认为这是由我在 Xcode 中创建应用程序时创建的自动 Interface Builder 模板引起的,因此我从项目中删除了 .nib 文件。然而窗口仍然被创建。

包含对窗口的引用的唯一行位于标题:

NSWindow *window;
@property (assign) IBOutlet NSWindow *window;

和实现文件中:

@synthesize window;

两者都是自动添加的,我没有写这个。

如何阻止应用程序创建窗口?我什至尝试从代码中删除对 window 的所有引用,包括 NSWindow *window,但窗口仍然被创建。

我现在的临时解决办法是在应用程序中调用 [window close];,但肯定有更好的方法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很酷不放纵 2024-12-02 16:43:46

我怀疑您的代码中没有任何内容正在创建窗口。当您创建新的 Cocoa Xcode 应用程序时,Xcode 会为您的界面设置一个 XIB。在界面生成器中打开 MainMenu.xib(应位于“资源”下)并删除它默认抛出的窗口。

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.

夏天碎花小短裙 2024-12-02 16:43:46

如果您不想显示窗口,您可以考虑在后台运行应用程序。这将阻止窗口出现。

为了在后台运行您的应用程序,请在应用程序的 PLIST 文件中将 YES 设置为“应用程序仅在后台”

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

做个ˇ局外人 2024-12-02 16:43:46

在 NSDocument 子类的 windowNibName 方法中返回 false。

Return false in your NSDocument subclass' windowNibName method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文