0

In AppDelegate.swift I've declared an NSStatusBar object like this:

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

and works fine, but I want to change the title in the viewController.swift

I trying this (In view controller):

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

But the title not change... how can I do this???

4
  • Try creating a function in your AppDelegate that changes the title, and then call that from your vc: AppDelegate().changeStatusTitle("ELO") Commented May 27, 2019 at 14:46
  • 1
    I don't think that calling functions that belong to your AppDelegate from another class would be good coding style Commented May 27, 2019 at 14:47
  • Hi @Koen I get the same result as my code above Commented May 27, 2019 at 15:01
  • @RX9 the app delegate is the root singleton of the app that's there in large part to handle objects and functions that don't have any other better owning view controller. Accessing data from and calling functions in the app delegate is a very standard coding practice. The problem OP is having is that he isn't accessing the singleton instance. Commented May 27, 2019 at 16:57

1 Answer 1

1

Get the delegate object from NSApplication, the default initializer AppDelegate() creates a new unrelated instance.

let appDelegate = NSApp.delegate as! AppDelegate
appDelegate.statusItem.button?.title = "ELO: \(parsing2.chess_daily.last.rating)"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.