首先继承 NSViewController,所以你的每个子视图都有一个控制器。在用户单击按钮以在视图之间切换时的操作方法中,您可以使用适当的类和 nib 创建一个新的视图控制器(在窗口控制器的 ivar 中保留对它的引用)。视图控制器充当 nib 的所有者。然后你所要做的就是在主窗口中添加视图控制器的视图作为子视图,然后你就设置好了。
这是一个简单的例子。在执行一些不相关的任务后,在主窗口控制器中从操作方法(以及启动后)调用它;唯一棘手的部分是修补响应者链(如果幸运的话,您可能不需要这样做)。
- (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]];
}