How to change NSStatusItem out of class

≯℡__Kan透↙ 提交于 2019-12-12 04:54:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!