NSViewController をサブクラス化することから始めて、各サブビューにコントローラーがあるようにします。ユーザーがボタンをクリックしてビューを切り替えるときのアクション メソッドで、適切なクラスと nib を使用して新しいビュー コントローラーを作成できます (ウィンドウ コントローラーの ivar で参照を保持します)。ビュー コントローラーは nib の所有者として機能します。次に、View Controller のビューをサブビューとしてメイン ウィンドウに追加するだけで、設定は完了です。
簡単な例を次に示します。これは、いくつかの無関係なタスクを実行した後、アクション メソッド (および起動後) からメイン ウィンドウ コントローラーで呼び出されます。唯一のトリッキーな部分は、レスポンダー チェーンにパッチを当てることです (運が良ければ、これを行う必要はないかもしれません)。
- (void)_setAccessoryViewControllerFromTag:(NSInteger)tag;
{
if ( _accessoryContentViewController != nil )
{
[self setNextResponder:[_accessoryContentViewController nextResponder]];
[_accessoryContentViewController release];
}
switch ( tag )
{
case 0:
_accessoryContentViewController = [[RLGraphsViewController alloc] initWithNibName:@"GraphsView" bundle:nil];
break;
case 1:
_accessoryContentViewController = [[RLSummaryViewController alloc] initWithNibName:@"SummaryView" bundle:nil];
break;
case 2:
_accessoryContentViewController = [[RLEquipmentViewController alloc] initWithNibName:@"EquipmentView" bundle:nil];
break;
default:
_accessoryContentViewController = [[RLLocationsViewController alloc] initWithNibName:@"LocationsView" bundle:nil];
break;
}
[_accessoryContentViewController setNextResponder:[self nextResponder]];
[self setNextResponder:_accessoryContentViewController];
[self.accessoryView setContentView:[_accessoryContentViewController view]];
}