I'm doing some crazy multiple documents inside a single window stuff with the document-based architecture and I'm 95% done.
I have this two-tier document architecture, where a parent document opens and configures the window, providing a list of "child" documents. When the user selects one of the children, that document is opened with the same window controller and it places a NSTextView
in the window. The window controller's document association is changed so that the "edited dot" and the window title track the currently selected document. Think of an Xcode project and what happens when you edit different files in it.
To put the code in pseudo form, a method like this is invoked in the parent document when a child document is opened.
-(void)openChildDocumentWithURL:(NSURL *)documentURL { // Don't open the same document multiple times NSDocument *childDocument = [documentMapTable objectForKey:documentURL]; if (childDocument == nil) { childDocument = [[[MyDocument alloc] init] autorelease]; // Use the same window controller // (not as bad as it looks, AppKit swaps the window's document association for us) [childDocument addWindowController:myWindowController]; [childDocument readFromURL:documentURL ofType:@"Whatever" error:NULL]; // Cache the document [documentMapTable setObject:childDocument forKey:documentURL]; } // Make sure the window controller gets the document-association swapped if the doc came from our cache [myWindowController setDocument:childDocument]; // Swap the text views in NSTextView *currentTextView = myCurrentTextView; NSTextView *newTextView = [childDocument textView]; [newTextView setFrame:[currentTextView frame]]; // Don't flicker [splitView replaceSubview:currentTextView with:newTextView]; if (currentTextView != newTextView) { [currentTextView release]; currentTextView = [newTextView retain]; } }
This works, and I know the window controller has the correct document association at any given time since the change dot and title follow whichever document I'm editing.
However, when I hit save, (CMD+S, or File -> Save/Save As) it wants to save the parent document, not the current document (as reported by [[NSDocumentController sharedDocumentController] currentDocument]
and as indicated by the window title and change dot).
From reading the NSResponder
documentation, it seems like the chain should be this:
Current View -> Superview (repeat) -> Window -> WindowController -> Document -> DocumentController -> Application.
I'm unsure how the document based architecture is setting up the responder chain (i.e. how it's placing NSDocument
and NSDocumentController
into the chain) so I'd like to debug it, but I'm not sure where to look. How do I access the responder chain at any given time?
The responder chain is the series of events that happen once we start interacting with the application on the iPhone. As an example, whenever we tap on an UITextfield, the whole series of responder initiates. It happens because of elements like UIView, UIViewController, UIApplication subclass UIResponder.
In iOS, the Responder Chain is the name given to an UIKit-generated linked list of UIResponder objects, and is the foundation for everything regarding events (like touch and motion) in iOS.
An abstract interface for responding to and handling events.
A responder object is any instance of the UIResponder class, and common subclasses include UIView , UIViewController , and UIApplication . Responders receive the raw event data and must either handle the event or forward it to another responder object.
You can iterate over the responder chain using the nextResponder method of NSResponder. For your example, you should be able to start with the current view, and then repeatedly print out the result of calling it in a loop like this:
NSResponder *responder = currentView; while ((responder = [responder nextResponder])) { NSLog(@"%@", responder); }
Here is another version for Swift users:
func printResponderChain(_ responder: UIResponder?) { guard let responder = responder else { return; } print(responder) printResponderChain(responder.next) }
Simply call it with self to print out the responder chain starting from self.
printResponderChain(self)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With