如何在类外更改NSStatusItem

如何在类外更改NSStatusItem

本文介绍了如何在类外更改NSStatusItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我像这样在名为StatusMenuController的类中创建一个NSStatusItem

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.

我在AppDelegate中引用了此类

I reference this class in AppDelegate

let statusMenu = StatusMenuController()

在其他课程中添加更改

statusMenu.statusItem.title = "24:31"

我希望25:00的文本更改为"24:31",但看起来像这样.屏幕截图

I expect the text 25:00 will change to "24:31",but it looks like this.screenshot

系统创建另一个NSStatusItem.我该如何更改在StatusMenuController中创建的那个.

System create another NSStatusItem. How can I change the one which created in StatusMenuController.

推荐答案

OOPer的评论正确.我找到了一个从Storyboard创建另一个StatusMenuController的对象.我删除对象,然后一切正常.

OOPer‘s comment is right. I found a object which create another StatusMenuController from Storyboard. I delete the object,Then everything is OK.

这篇关于如何在类外更改NSStatusItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 05:37