Objective c 如何让NSViewController';s视图';s的内容取决于它的方式';它初始化了吗?
这实际上是一个由两部分组成的问题,希望我的解释清楚: 我有一个NSViewController,它可以配置为在其视图的一部分上显示不同的自定义视图。例如,其视图可以显示CustomViewA或CustomViewB 我可以通过为每个自定义视图创建一个NSViewController,并使用处理CustomViewA的NSViewController或处理CustomViewB的NSViewController初始化MyViewController来实现这一点。我使用NSBox并将其contentView设置为给定NSViewController提供的视图 这种方法的问题是,我有一个NSBox,它的contentView将保存“MyView”,而在“MyView”中,我有另一个NSBox,它将保存CustomViewA或CustomViewB 另一个问题是,我希望MyViewController同时处理CustomViewA和CustomViewB,而不是为它们中的每一个使用单独的NSViewController 以下是我当前解决方案的示例代码:Objective c 如何让NSViewController';s视图';s的内容取决于它的方式';它初始化了吗?,objective-c,cocoa,nsviewcontroller,Objective C,Cocoa,Nsviewcontroller,这实际上是一个由两部分组成的问题,希望我的解释清楚: 我有一个NSViewController,它可以配置为在其视图的一部分上显示不同的自定义视图。例如,其视图可以显示CustomViewA或CustomViewB 我可以通过为每个自定义视图创建一个NSViewController,并使用处理CustomViewA的NSViewController或处理CustomViewB的NSViewController初始化MyViewController来实现这一点。我使用NSBox并将其content
// How I initialize the NSViewControllers
CustomViewControllerA* cvc = [[CustomViewControllerA alloc] initWithNibName:@"CustomViewA" bundle:nil];
MyViewController* controller = [[MyViewController alloc] initWithCustomViewController:cvc nibName:@"MyView" bundle:nil];
//...
// In Controller of main view
- (void)awakeFromNib
{
// container is an NSBox*
[self.container setContentView:[self.myViewController view]];
}
//...
// In MyViewController
-(void)awakeFromNib
{
// content is an NSBox*
[self.content setContentView:[self.customViewController view]];
}
我如何让我的CustomViewA和CustomViewB在MyView.nib中运行,并且它们都使用MyViewController作为控制器
如何让主视图保存MyView而不是NSBox
提前谢谢 试试这个:
// In MyViewController.m
- (void) awakeFromNib {
[self.view addSubview:[self.customViewController.view]];
}
您的MyViewController必须具有以下参数:
// In MyViewController.h
@interface MyViewController : NSViewController {
IBOutlet NSViewController* customViewController;
}
下面是我可能会着手设置的方法
@interface MyViewController : NSViewController
{
IBOutlet NSView* customView; //initially points to an NSBox or generic custom view from the nib file
CustomViewControllerA* viewControllerA;
CustomViewControllerB* viewControllerB;
}
@end
@implementation MyViewController
- (void)switchToCustomViewController:(NSViewController*)newCustomViewController
{
//Make sure the subview gets put in the right location
[newCustomViewController.view setFrame:customView.frame];
[self.view replaceSubview:customView withView:newCustomViewController.view];
customView = newCustomViewController.view;
}
//I usually use loadView in NSViewControllers rather than awakeFromNib, but either works
- (void)loadView
{
[super loadView];
viewControllerA = [[CustomViewControllerA alloc] initWithNibName:@"CustomViewA" bundle:nil];
viewControllerB = [[CustomViewControllerB alloc] initWithNibName:@"CustomViewB" bundle:nil];
[self switchToCustomViewController:viewControllerA];
}
@end
因此,与将自定义视图放在NSBox中不同,此设置将在nib中包含一个视图作为临时占位符,该占位符在您第一次切换到某个自定义视图时被删除。然后,当您想要交换时,只需使用您想要切换到的控制器调用
-switchToCustomViewController:
。从那时起,当您切换时,您只需将一个自定义视图替换为另一个自定义视图,作为MyViewController视图的直接子视图。如果您希望能够指定初始使用的视图,只需创建一个init方法或简单属性,可以设置该方法或属性来告诉MyViewController哪个自定义视图应首先使用。NSViewController并不真正用于处理多个交换的视图。它主要用于处理从Nib/Xib加载的单个视图,管理顶级对象的内存,并提供方便的绑定功能。典型的使用模型是为每个视图创建NSViewController子类,就像您当前所做的那样。谢谢,我会尝试的!但有一个问题。。。MyViewController在用户单击按钮时交换视图。我只知道如何使用NSBox setCustomView执行此操作:在这种方法中,如何在用户单击时交换视图?再次调用[self.view addSubview:…]或多或少。您可以再次调用addSubview,但也需要在添加的原始子视图上调用[NSView removeFromSuperview]。谢谢您的提示。除了自定义视图的自动大小调整不适用于此方法之外,此方法工作正常。当我切换开关时,它将正确调整大小,因为我正在设置帧。这是否意味着每次我都要听视图的调整大小并设置帧?如果是的话。。。将自定义视图设置为NSBox的contentView时,会自动调整大小。切换子视图而不是使用NSBox并切换其contentView是否有好处?只想补充一点,我的视图确实启用了“自动调整子视图大小”标志。在Interface Builder中,我在视图中添加了一个NSBox,以测试它是否会被调整大小,并且它是。只有以编程方式添加的子视图不会自动调整大小。