0

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?

JonGrimes20
  • 115
  • 9
  • 1
    You need to edit your question and provide a [mcve] – Leo Dabus Mar 13 '22 at 21:29
  • Where are you writing your data? Looks like when you reload your table view you are not updating the data source again from disk – Leo Dabus Mar 13 '22 at 21:36
  • 1
    I forgot to create a new object and tried to rely on my query inside of viewDidLoad() thank you for pointing that out. Once I created a new object and appended it to the array for populating the table it now works, thank you very much. – JonGrimes20 Mar 13 '22 at 21:58

1 Answers1

0

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.

Aviel Gross
  • 9,770
  • 3
  • 52
  • 62