我正在尝试向状态栏添加一个项目,但当我启动应用程序时,该项目只会在左上角出现一瞬间,然后很快就会消失。
我已经看过文档了,我可以看到最近发生了一些变化,比如statusItem.title变成了statusItem.button?.title。但似乎不会遗漏任何其他东西。有什么帮助吗?
下面是我的代码:
var statusItem : NSStatusItem? = nil
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
statusItem.button?.title = "Connect!"
}发布于 2019-02-20 04:11:02
啊太棒了。这已经成功了!谢谢萨利赫。在使用了我们的两个代码之后,我的代码似乎可以在顶部使用var声明,但没有NSMenuDelegate实例。我的问题似乎是我在说:
让statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
我要做的就是去掉“let”,然后说:
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
发布于 2019-02-20 03:04:05
NSMenuDelegate
NSStatusBar.system.statusItem(withLength:NSStatusItem.variableLength):NSImage(named:NSImage.Name("StatusBarButtonImage")) AppDelegate: NSObject,NSApplicationDelegate,NSMenuDelegate { let statusItem =applicationDidFinishLaunching函数通知(_ aNotification: NSMenuDelegate){ if let statusItem=applicationDidFinishLaunching{ //button.image =NSImage(named:NSImage.Name(“StatusBarButtonImage”))AppDelegate= "connect“//button.action =# button.title (doSomething(_:))} }
https://stackoverflow.com/questions/54772386
复制相似问题