ドキュメントベースのアーキテクチャを使用して、単一のウィンドウ内でクレイジーな複数のドキュメントを処理していますが、95% 完了しています。
親ドキュメントを開いてウィンドウを構成し、「子」ドキュメントのリストを提供する、この 2 層ドキュメント アーキテクチャがあります。ユーザーが子の 1 つを選択すると、そのドキュメントが同じウィンドウ コントローラーで開かれ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 -> アプリケーション。
ドキュメント ベースのアーキテクチャがレスポンダ チェーンをどのように設定しているか (つまり、どのようにチェーンに配置NSDocument
しNSDocumentController
ているか) がわからないので、デバッグしたいと思いますが、どこを見ればよいかわかりません。いつでもレスポンダー チェーンにアクセスするにはどうすればよいですか?