如何检查响应链? [英] How to inspect the responder chain?

查看:148
本文介绍了如何检查响应链?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有两个文件夹,一个文件夹,一个文件夹,其中父文档打开并配置该窗口,提供子文档的列表。当用户选择一个孩子时,该文档用相同的窗口控制器打开,并在窗口中放置一个 NSTextView 。改变窗口控制器的文档关联,使得编辑的点和窗口标题跟踪当前选择的文档。想想一个Xcode项目,以及当你编辑不同的文件时会发生什么。



为了把代码放在伪表单中,在父文档中调用这样的方法一个子文档被打开。

   - (void)openChildDocumentWithURL:(NSURL *)documentURL {
// Don'多次打开同一文档
NSDocument * childDocument = [documentMapTable objectForKey:documentURL];
if(childDocument == nil){
childDocument = [[[MyDocument alloc] init] autorelease];
//使用相同的窗口控制器
//(不像看起来那样糟糕,AppKit为我们交换窗口的文档关联)
[childDocument addWindowController:myWindowController];
[childDocument readFromURL:documentURL ofType:@Whatevererror:NULL];

//缓存文档
[documentMapTable setObject:childDocument forKey:documentURL];
}

//如果文档来自我们的缓存,确保窗口控制器获得文档关联交换
[myWindowController setDocument:childDocument];

//在
中交换文本视图NSTextView * currentTextView = myCurrentTextView;
NSTextView * newTextView = [childDocument textView];
[newTextView setFrame:[currentTextView frame]]; //不闪烁

[splitView replaceSubview:currentTextView with:newTextView];

if(currentTextView!= newTextView){
[currentTextView release];
currentTextView = [newTextView retain];
}
}

这样工作,我知道窗口控制器有



但是,当我点击save,(CMD + S或File)时, - >保存/另存为)它想保存父文档,而不是当前文档(由 [[NSDocumentController sharedDocumentController] currentDocument] 报告,并且如窗口标题



通过阅读 NSResponder 文档,看起来链应该是这样的:



当前视图 - > Superview(重复) - >窗口 - > WindowController - >文档 - > DocumentController - >应用程序。
$ b

我不确定如何基于文档的架构设置响应链(即如何放置 NSDocument NSDocumentController 到链中)所以我想调试它,但我不知道在哪里看。

解决方案

您可以使用nextResponder NSResponder的方法。对于你的例子,你应该能够从当前视图开始,然后重复打印输出调用它的结果,如下:

  NSResponder * responder = currentView; 
while((responder = [responder nextResponder])){
NSLog(@%@,responder);
}


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?

解决方案

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);
}

这篇关于如何检查响应链?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆