如何让 NSViewController 的视图内容取决于它的初始化方式?

发布于 2024-09-09 21:44:55 字数 1329 浏览 2 评论 0原文

这实际上是一个由两部分组成的问题,希望我的解释很清楚:

我有一个 NSViewController ,可以将其配置为在其视图的一部分上显示不同的自定义视图。例如,其视图可以显示 CustomViewA 或 CustomViewB。

我能够通过为每个自定义视图创建一个 NSViewController 并使用处理 CustomViewA 的 NSViewController 或处理 CustomViewB 的 NSViewController 初始化 MyViewController 来完成这项工作。我使用 NSBox 并将其 contentView 设置为给定 NSViewController 提供的视图。

这种方法的问题是我有一个 NSBox,其 contentView 将保存“MyView”,然后在“MyView”内我有另一个 NSBox,它将保存 CustomViewA 或 CustomViewB。

另一个问题是我希望 MyViewController 能够处理 CustomViewA 和 CustomViewB,而不是为它们每个都有一个单独的 NSViewController。

以下是我当前解决方案的示例代码:

// 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]];
}

How can I have my CustomViewA 和 CustomViewB live inside MyView.nib 并且它们都使用 MyViewController 作为其控制器?

我怎样才能让主视图持有 MyView 而不是 NSBox?

提前致谢!

This is actually a two part question, hope my explanation is clear:

I have an NSViewController which can be configured to show a different custom view on part of its view. For example, its view can show either CustomViewA or a CustomViewB.

I was able to make this work by creating an NSViewController for each of the custom views and initializing MyViewController with either an NSViewController which handles the CustomViewA or an NSViewController which handles the CustomViewB. I use an NSBox and set its contentView to the view provided by the given NSViewController.

The problem with this approach is that I have an NSBox who's contentView will hold the "MyView" and then inside "MyView" I have another NSBox which will hold either the CustomViewA or the CustomViewB.

The other problem is that I'd like MyViewController to handle both the CustomViewA and the CustomViewB, opposed to having a separate NSViewController for each one of them.

Here is sample code of my current solution:

// 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]];
}

How can I have my CustomViewA and CustomViewB live inside MyView.nib and both of them use MyViewController as their Controller?

How could I have the main view hold a MyView instead of an NSBox?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屌丝范 2024-09-16 21:44:55

这是我可能会进行设置的方式。

@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 中,而是将一个视图包含在笔尖中作为临时占位符,当您第一次切换到其中一个自定义视图时,该占位符就会被删除。然后,当您想要交换时,只需使用您想要切换到的控制器调用 -switchToCustomViewController: 即可。从那时起,当您切换时,您只需将一个自定义视图交换为另一个作为 MyViewController 视图的直接子视图。如果您希望能够指定首先使用哪一个视图,只需创建一个 init 方法或简单的属性,可以将其设置为告诉 MyViewController 应该首先使用哪个自定义视图。

Here's the way I would probably go about setting this up.

@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

So, rather than putting the custom view inside an NSBox, this setup would have a view included in the nib as a temporary placeholder that just gets removed the first time you switch to one of the custom views. Then, when you want to swap, you just call -switchToCustomViewController: with the controller you want to switch to. From then on, when you switch, you're just swapping one custom view for the other as a direct subview of MyViewController's view. If you want to be able to specify which one should be the one used initially, just create an init method or simple property that can be set to tell MyViewController which custom view should be the one used first.

殊姿 2024-09-16 21:44:55

试试这个:

// In MyViewController.m
- (void) awakeFromNib {
  [self.view addSubview:[self.customViewController.view]];
}

你的 MyViewController 将必须有一个参数,例如:

// In MyViewController.h
@interface MyViewController : NSViewController {
  IBOutlet NSViewController* customViewController;
}

Try this:

// In MyViewController.m
- (void) awakeFromNib {
  [self.view addSubview:[self.customViewController.view]];
}

and your MyViewController will have to have a parameter such as:

// In MyViewController.h
@interface MyViewController : NSViewController {
  IBOutlet NSViewController* customViewController;
}
莫相离 2024-09-16 21:44:55

NSViewController 并不是真正要处理换入换出的多个视图。它主要用于处理从 Nib/Xib 加载的单个视图、管理顶级对象的内存并提供方便的绑定功能。典型的使用模型是为每个视图创建 NSViewController 子类,就像您当前所做的那样。

NSViewController isn't really intended to handle multiple views that are swapped in and out. It is mostly for handling a single view loaded from a Nib/Xib, managing memory of the top-level objects and providing convenient bindings functionality. The typical usage model is to subclass NSViewController for each view, as you are currently doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文