我在一个 xib 中有一个 CustomView,在两个不同的 xib 中有两个不同的 View 。 我想在一个 CustomeView 中依次显示这两个 View 。 我有一个 NSView 对象,它连接到 .xib 文件中的 CustomView
@property (retain) IBOutlet NSView *mySubview; @property (retain) NSViewController *viewController;
打开一个 View 的方法是:
-(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 中打开另一个 View 的方法是:
-(IBAction)selectTicketCancellation:(id)sender
{
_viewController=[[NSViewController alloc] initWithNibName:@"CancelTicket" bundle:nil];
//loading CancelTicket xib in custom view of NormalUserWindow
[_mySubview addSubview:[_viewController view]];
}
当我第一次打开任何 View 时,它会在 CustomView 中正确显示,但是当我尝试第二次打开第二个 View 或同一 View 时,它会与之前打开的 View 重叠。
我试过了
[_mySubview removeFromSuperview]
它正在完全删除“mySubview”,我的意思是无论当前加载的 View 是什么,它都会被删除,但在执行'[_mySubview removeFromSuperview]'
之后不允许显示任何 View 。
最佳答案
您必须仅删除已从 View Controller 添加的 View 。请尝试使用以下代码。
-(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]
将从 View 层次结构中删除您的主机 View (即显示其他 View Controller 的 View ),这解释了“不允许显示任何其他 subview 部分” ”。
关于cocoa - 在单个自定义 View 中将一个 View 切换到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15655424/