在一个自定义视图中将一个视图切换到另一个视图
问题描述:
我在一个xib中有一个CustomView,在两个不同的xib中有两个不同的视图。 我想在一个CustomeView中一个接一个地显示这两个视图。 我的NSView的目的,其被连接到在CustomView文件的.xib在一个自定义视图中将一个视图切换到另一个视图
@property (retain) IBOutlet NSView *mySubview; @property (retain) NSViewController *viewController;
方法来打开一个观点是:
-(IBAction)selectBookTicket:(id)sender
{
//setting status label to nil
_viewController=[[NSViewController alloc] initWithNibName:@"BookTicket" bundle:nil];
//loading bookTicket xib in custom view of NormalUserWindow
[_mySubview addSubview:[_viewController view]];
}
法打开另一个视图相同的CustomView是:
-(IBAction)selectTicketCancellation:(id)sender
{
_viewController=[[NSViewController alloc] initWithNibName:@"CancelTicket" bundle:nil];
//loading CancelTicket xib in custom view of NormalUserWindow
[_mySubview addSubview:[_viewController view]];
}
当我第一次打开任何视图时,它在CustomView中正确显示,但是当我尝试第二次打开第二个视图或同一个视图时,它会在先前打开的视图上重叠。
我试图
[_mySubview removeFromSuperview]
它移除“mySubview”完全,我的意思是什么都认为当前加载它得到消除,但它不是允许显示任何意见之后'[_mySubview removeFromSuperview]'
得到执行。
答
您只能删除已从视图控制器添加的视图。改为尝试下面的代码。
-(IBAction)selectTicketCancellation:(id)sender
{
[[_viewController view] removeFromSuperView];
_viewController=[[NSViewController alloc] initWithNibName:@"CancelTicket" bundle:nil];
//loading CancelTicket xib in custom view of NormalUserWindow
[_mySubview addSubview:[_viewController view]];
}
执行[_mySubview removeFromSuperview]
将删除宿主视图(即,也就是说从其他视图控制器显示视图的一个)从视图层次结构,这解释了“不允许显示任何其他子视图的一部分”。