Search code examples
objective-cnsnotificationcenternsdocument

Get the right window for NSDocument's readFromData:::


I've successfully implemented a NSDocument class in my Cocoa App which is also able to save data properly. But I have problems when it's about opening files.

This is my basic setup:

NSDocument MyFile ==xib File=> MyFile.xib

NSObject FileClass as processing class

FileClass is a class that compresses/decompresses files and is connected to the window's textfield in MyFile.xib. Saving is done via the default NSNotificationCenter. The data needs to be processed in this class because it does not contain raw NSStrings.

Let's say I have three documents that need to opened I have three NSDocument instances that now send their data via the NSNotificationsCenter. Because I have three NSDocuments I have three windows and three FileClass instances. Now it's just luck if the correct FileClass receives the data because I have no possibility to filter whether the received data is proposed for this window. A random FileClass now processes the file and sets the text of its associated Text View. So Window 1 could show the data that were thought to be shown in Window 3.

The data flow:

NSDocument => readFromData => NSNotificationCenter => random FileClass => associated TextView.

To narrow it down: How do I route the data to its correct window and how/where do I include FileClass in this construct?

Thanks in advance!

Paul


Solution

  • It's an indication for a major design flaw if you have to force your document data through the notification center to your viewing classes. Why do you have to do that? What issues are you trying to circumvent?