Is there a better way to access the Document from its subviews?

190 views Asked by At

I have some classes located on my Document such as NSNotificationCenter and NSUndoManager that I need access to from my subviews.

Right now I can access them by doing something like this:

NSUndoManager *undoManager = [[[[[self view] window] windowController] document] undoManager];

Is there a better approach I don't know about? Thanks.

1

There are 1 answers

4
Caleb On BEST ANSWER

Why does a view need to worry about the undo manager? Seems like some refactoring might be in order there.

Normally, the view might trigger some action in the first responder. That action then gets passed up the responder chain until it finds an object, such as the window controller or document, that responds to that action. An 'undo' event generally affects the data model, so the document would handle something like that. When the user undoes a previous operation, the model is reverted back to a prior state, and the view hierarchy reflects the change.

Is it possible in your case to move the responsibility for dealing with the undo manager up to the document?

The same could apply to the notification center. If the document has a notification center, why is the view trying to use it? Can you leverage the responder chain as described above to get a message to the document? There's also a default notification center that you can get with +defaultNotificationCenter if that's more appropriate.