2010-11-22 35 views
16

我正在做一些疯狂的多个文件在单个窗口内的文件为基础的架构的东西,我95%完成。如何检查响应者链?

我有这种双层文档体系结构,其中父文档打开并配置窗口,提供“子”文档列表。当用户选择其中一个孩子时,该文档将使用相同的窗口控制器打开,并在窗口中放置一个NSTextView。窗口控制器的文档关联被更改,以便“编辑的点”和窗口标题跟踪当前选定的文档。想想一个Xcode项目,以及在编辑其中的不同文件时会发生什么。

要将代码置于伪形式,当打开子文档时,会在父文档中调用类似这样的方法。

-(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]; 
    } 
} 

这工作,我知道,窗控制器具有在任何给定的时间正确的文件关联,因为变化点和标题跟随我编辑的文件为准。但是,当我点击保存时,(CMD + S或文件 - >保存/另存为),它想要保存父文档,而不是当前文档(如[[NSDocumentController sharedDocumentController] currentDocument]报告的那样,并且如窗口标题和改变点)。

从阅读NSResponder文档,好像链条应该是这样的:

当前视图 - >上海华盈(重复) - >窗口 - > WindowController - >文件 - > DocumentController - >应用程序。

我不确定基于文档的架构如何设置响应者链(即如何将NSDocumentNSDocumentController置入链中),所以我想调试它,但我不确定在哪里寻找。我如何在任何特定时间访问响应者链?

回答

38

您可以使用NSResponder的nextResponder方法遍历响应者链。对于你的榜样,你应该能够开始与当前视图,然后反复打印出结果,称这是在这样的循环:

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

谢谢,我不知道这是怎么发生在我身上,我非常专注于直接抓取整个堆栈的想法。 – d11wtq 2010-11-22 03:39:21

6

这里是另一个版本的雨燕用户:

func printResponderChain(_ responder: UIResponder?) { 
    guard let responder = responder else { return; } 

    print(responder) 
    printResponderChain(responder.next) 
} 

只需用自己的名称来打印出自己的响应者链。

printResponderChain(self) 
3

您还可以使用UIResponder的任何子类可以使用的适当方法向类UIResponder添加一个类别。

@interface UIResponder (Inspect) 

- (void)inspectResponderChain; // show responder chain including self 

@end 

@implementation UIResponder (Inspect) 

- (void)inspectResponderChain 
{ 
    UIResponder *x = self; 
    do { 
     NSLog(@"%@", x); 
    }while ((x = [x nextResponder])); 
} 
@end 

比你可以在代码中的某处使用此方法,下面的例子:

- (void)viewDidLoad { 
    ... 
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    [self.view addSubview:myView]; 
    [myView inspectResponderChain]; // UIView is a subclass of UIResponder 
    ... 
} 
2

斯威夫特3:

func printResponderChain(from responder: UIResponder?) { 
    var responder = responder 
    while let r = responder { 
     print(r) 
     responder = r.next 
    } 
} 


printResponderChain(from: view) 
1

我会改进的响应类别回答了一下,通过使用一种在调试时感觉更“可用”的类方法(您不需要在特定视图或其他任何方面打破)。

代码适用于Cocoa,但应易于移植到UIKit。

@interface NSResponder (Inspect) 

+ (void)inspectResponderChain; 

@end 

@implementation NSResponder (Inspect) 

+ (void)inspectResponderChain 
{ 
    NSWindow *mainWindow = [NSApplication sharedApplication].mainWindow; 

    NSLog(@"Responder chain:"); 
    NSResponder *responder = mainWindow.firstResponder; 
    do 
    { 
    NSLog(@"\t%@", [responder debugDescription]); 
    } 
    while ((responder = [responder nextResponder])); 
} 

@end