cocoa - 在单个自定义 View 中将一个 View 切换到另一个 View

标签 cocoa xcode4.5 nsview nsviewcontroller

我在一个 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/

相关文章:

objective-c - 确定哪个 NSView 实例发起了 mouseDown :

arrays - 这是实现 NSImage 数组的合适方法吗?

iphone - Cocos2D标签不可见,黑屏

ios - 在没有导航 Controller 的情况下推送 UIViewController

objective-c - NSView 子类中的可选文本

swift - 设置 NSView 的比例

ios - 什么时候使用 NSSet 而不是 NSArray 更好?

macos - 翻转 NSView 鼠标坐标

swift - 在非沙盒中使用 Cocoa NSSavePanel 会导致断言失败

ios - 将应用程序升级到 iOS 6 时的 Root View Controller