问题
I create a NSStatusItem in a class named StatusMenuController like this
class StatusMenuController: NSObject {
let statusItem = NSStatusBar.system().statusItem(withLength: NSVariableStatusItemLength)
override func awakeFromNib() {
let icon = NSImage(named: "MenuBar")
icon?.isTemplate = true // best for dark mode
if let button = statusItem.button {
button.image = icon
button.action = #selector(StatusMenuController.showPomoNow)
button.sendAction(on: [.leftMouseUp, .rightMouseUp])
button.target = self
}
statusItem.title = "25:00"
}
I can change the title easily in this class. It will change like I expected.
I reference this class in AppDelegate
let statusMenu = StatusMenuController()
Add change it in other class
statusMenu.statusItem.title = "24:31"
I expect the text 25:00 will change to "24:31",but it looks like this.screenshot
System create another NSStatusItem. How can I change the one which created in StatusMenuController.
回答1:
OOPer‘s comment is right. I found a object which create another StatusMenuController from Storyboard. I delete the object,Then everything is OK.
来源:https://stackoverflow.com/questions/45653330/how-to-change-nsstatusitem-out-of-class