Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inspect the responder chain?

Tags:

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?

like image 521
d11wtq Avatar asked Nov 22 '10 01:11

d11wtq


People also ask

Can you explain the responder chain?

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.

What is the responder chain iOS?

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.

What is UI responder?

An abstract interface for responding to and handling events.

What is responder in swift?

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.


2 Answers

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); } 
like image 149
Justin Spahr-Summers Avatar answered Oct 27 '22 06:10

Justin Spahr-Summers


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) 
like image 29
Kie Avatar answered Oct 27 '22 04:10

Kie