我想为 macOS 制作状态栏,但运行应用程序后标题显示并立即消失
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
let statusItem = NSStatusBar.system().statusItem(withLength: NSVariableStatusItemLength)
statusItem.title = "Hello"
}
我认为引用有问题,但不知道如何解决这个问题。
事实上,您需要对状态项进行强有力的引用
var statusItem : NSStatusItem!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
statusItem = NSStatusBar.system().statusItem(withLength: NSVariableStatusItemLength)
statusItem.title = "Hello"
}
不过我建议使用闭包来初始化状态项
let statusItem : NSStatusItem = {
let item = NSStatusBar.system().statusItem(withLength: NSVariableStatusItemLength)
item.title = "Hello"
return item
}()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)