I tried to add a simple status menu to the status bar with swift but it will not be shown.
with objective-c this worked:
AppDelegate.h
@int
The problem here is that statusItem
is going out of scope after applicationDidFinishLaunching
finishes execution which in turn releases the object. This is not the case in your Objective-C code because the statusItem
variable is declared at class level.
This should make your Swift code work:
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet var statusMenu: NSMenu;
var statusItem: NSStatusItem?;
func applicationDidFinishLaunching(aNotification: NSNotification?) {
let bar = NSStatusBar.systemStatusBar()
statusItem = bar.statusItemWithLength(CGFloat(NSVariableStatusItemLength))
statusItem!.title = "Status Menu"
statusItem!.menu = statusMenu
statusItem!.highlightMode = true
}
}