gpt4 book ai didi

cocoa - 从 `NSStatusItem` 创建的 `main()` 未显示在系统状态栏上

转载 作者:行者123 更新时间:2023-12-03 17:31:09 24 4
gpt4 key购买 nike

我正在尝试使用 NSStatusItem 制作一个简单的 macOS Cocoa 应用程序,以在系统状态栏上创建可单击的图标。但是,当我启动应用程序时,我收到此警告并且图标不显示:

2020-03-03 14:43:11.564 Mocha_bug_example[936:39572] CGSGetActiveMenuBarDrawingStyle((CGSConnectionID)[NSApp contextID], &sCachedMenuBarDrawingStyle) returned error 268435459 on line 46 in NSStatusBarMenuBarDrawingStyle _NSStatusBarGetCachedMenuBarDrawingStyle(void)

这是我的应用程序的一个最小的可重现示例:

#import <AppKit/AppKit.h>

NSStatusItem* statusItem;

int main (int argc, char* argv[]) {
statusItem = [NSStatusBar.systemStatusBar statusItemWithLength: -1];
statusItem.button.title = @"foobar";
statusItem.visible = YES;

[NSApplication.sharedApplication run];
return 0;
}

我编译并运行了这样的示例:

MacBook-Air-5:Mocha ericreed$ clang -o Mocha_bug_example -framework AppKit -fobjc-arc Mocha_bug_example.m
MacBook-Air-5:Mocha ericreed$ ./Mocha_bug_example
2020-03-03 14:43:11.564 Mocha_bug_example[936:39572] CGSGetActiveMenuBarDrawingStyle((CGSConnectionID)[NSApp contextID], &sCachedMenuBarDrawingStyle) returned error 268435459 on line 46 in NSStatusBarMenuBarDrawingStyle _NSStatusBarGetCachedMenuBarDrawingStyle(void)
[Application hung until I pressed Ctrl+C]
^C
MacBook-Air-5:Mocha ericreed$

注意:禁用自动引用计数并在调用 run 后添加 [statusItem release]; 作为 this similar question建议没有明显的区别。

最佳答案

这是如何将状态栏项目添加到命令行应用程序 mac osx cocoa

将 apodidae 的答案改编为 Swift。只需将其放入 main.swift 文件中即可:

let app = NSApplication()
let statusItem = NSStatusBar.system.statusItem(withLength: -1)
statusItem.button!.title = "Hello, world!"
app.run()

我不明白 NSReleasePool 作为 apodidae 的更详细的细节,但它对我来说不需要它。

关于cocoa - 从 `NSStatusItem` 创建的 `main()` 未显示在系统状态栏上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60514459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com