我有一个NSWindowController
包含根视图。根视图有 4 NSButtons
,一些文本和图像。每个按钮都绑定到一个NSViewController
. 当我单击其中一个按钮时,会删除根视图并显示绑定到NSViewController
(我们称其为子视图)的视图。在子视图中,有一个NSButton
向窗口控制器发送通知以恢复根视图。这是我的代码(我删除了大部分无用的部分)
窗口控制器.h
@interface MainWindowController : NSWindowController { IBOutlet NSView* myTargetView; // bound to the whole view of the window NSView* viewRoot; NSViewController* myCurrentViewController; } - (IBAction)buttonClicked:(id)sender; // Not shown in the implementation - (void)changeViewController:(NSInteger)buttonTag; - (void)restoreRootView; @end
窗口控制器.m
- initWithPath:(NSString *)newPath { return [super initWithWindowNibName:@"MainWindow"]; } - (void)windowDidLoad { vwRoot = [[[[self window] contentView] subviews] objectAtIndex:0]; // set up notification observer, will call restoreRootView when receiving notification from NSViewController object } - (void)changeViewController:(NSInteger)buttonTag { [vwRoot retain]; [vwRoot removeFromSuperview]; if (myCurrentViewController != nil) [myCurrentViewController release]; switch (buttonTag) { case kView1: { View1Controller * viewOneController = [[View1Controller alloc] initWithNibName:kViewOneTile bundle:nil]; if (viewOneController != nil) { myCurrentViewController = viewOneController; } break; } case kView2: { // and so on... } } [myTargetView addSubview: [myCurrentViewController view]]; [[myCurrentViewController view] setFrame: [myTargetView bounds]]; } - (void)restoreRootView { [[myCurrentViewController view] removeFromSuperview]; [myTargetView addSubview:vwRoot]; [[vwRoot setFrame:[myTargetView bounds]]; }
不幸的是,当restoreRootView
被调用时,NSViewController
的视图被删除,但根视图不显示。