从 ViewController 访问 AppDelegate 中的 object

Access object in AppDelegate from ViewController

AppDelegate.swift 中,我声明了一个 NSStatusBar object,如下所示:

var statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
statusItem.button?.title = "chess"
statusItem.button?.target = self
statusItem.button?.action = #selector(showSettings)

并且工作正常,但我想更改 viewController.swift

中的标题

我正在尝试这个(在视图控制器中):

var appd = AppDelegate()
appd.statusItem.button?.title = "ELO: \(parsing2.chess_daily.last.rating)"

但是标题没有改变...我该怎么做???

NSApplication 获取委托对象,默认初始化程序 AppDelegate() 创建一个新的无关实例。

let appDelegate = NSApp.delegate as! AppDelegate
appDelegate.statusItem.button?.title = "ELO: \(parsing2.chess_daily.last.rating)"