Home > Blockchain >  how to reload NSTableView data from a different ViewController
how to reload NSTableView data from a different ViewController

Time:03-16

I'm working on a simple notes app for macOS. I have a home page which is has a NSTableView that displays all your notes, when you click the new note button a new View appears where you can create a new note. Once you click the note it adds the new note to the database and should reload the table view data, but I need to stop the current run and run the program again to see the changes.

I used this post to achieve the same effect on iOS but it seems to not work on MacOS

So how do I adapt:

override func viewDidLoad() {
    super.viewDidLoad
    NotificationCenter.default.addObserver(self, selector: #selector(loadList), name: NSNotification.Name(rawValue: "load"), object: nil)
}

In the home page VC

and the line:

NotificationCenter.default.post(name: NSNotification.Name(rawValue: "load"), object: nil)

Inside of the saveNewNote IBAction to work in macOS? also are you even able to use the NotificationCenter in macOS apps or is it only on iOS?

CodePudding user response:

NSNotificationCenter is part of Foundation framework, so it's definitely available on macOS.

You should be able to use it the same way you've been using it on iOS. If you have an IBAction called saveNewNote, inside that method you can posy the notification the way you wrote. In the view controller which owns the table, add the observer like you wrote, and reload the table...

If it doesn't work, we might need some code example of how you set it up on the Mac app the better understand what isn't working.

  • Related