NSViewController 和 Nib 中的多个子视图

发布于 2024-08-11 04:56:28 字数 990 浏览 13 评论 0原文

我很难理解如何使用 Interface Builder 和 NSViewController 加载视图。

我的目标是拥有一个满足以下描述的视图:顶部的顶栏(类似于工具栏,但不完全一样)跨越视图的整个宽度,下面是第二个“内容视图”。这个复合视图由我的 NSViewController 子类拥有。

为此使用 Interface Builder 是有意义的。我创建了一个视图笔尖,并向其中添加了两个子视图,并正确放置了它们(带有顶部栏和内容视图)。我已将File's Owner 设置为MyViewController,并连接了插座等。

我希望加载的视图(栏和内容)也在它们自己的笔尖中(这可能是让我绊倒的原因),并且这些笔尖将其自定义类设置为相应的 NSView 子类(如果适用)。我不确定将什么设置为他们的File's Owner(我猜测MyController,因为它应该是他们的所有者)。

唉,当我初始化 MyViewController 的实例时,我的笔尖实际上都没有显示。我已经将它正确添加到我的 Window 的 contentView 中(我已经检查过其他情况),实际上,事情已经加载了。也就是说,awakeFromNib 被发送到栏视图,但它不会显示在窗口中。我想我肯定在某个地方交叉了一些电线。也许有人可以伸出援手来缓解我的一些挫败感?

编辑一些代码来显示我正在做的事情

当我的应用程序完成启动时,从应用程序委托加载控制器:

MyController *controller = [[MyController alloc] initWithNibName:@"MyController" bundle:nil];
[window setContentView:[controller view]];

然后在我的 initWithNibName 中,除了暂时调用 super 之外,我什么也不做。

I'm having a difficult time wrapping my head around loading views with Interface Builder and NSViewController.

My goal is to have a view which meets the following description: Top bar at the top (like a toolbar but not exactly) which spans the entire width of the view, and a second "content view" below. This composite view is owned by my NSViewController subclass.

It made sense to use Interface Builder for this. I have created a view nib, and added to it two subviews, laid them out properly (with the top bar and the content view). I've set File's Owner to be MyViewController, and connected outlets and such.

The views I wish to load in (the bar and the content) are also in their own nibs (this might be what's tripping me up) and those nibs have their Custom Class set to the respective NSView subclass where applicable. I'm not sure what to set as their File's Owner (I'm guessing MyController as it should be their owner).

Alas, when I init an instance of MyViewController none of my nibs actually display. I've added it to my Window's contentView properly (I've checked otherwise), and actually, things sort of load. That is, awakeFromNib gets sent to the bar view, but it does not display in the window. I think I've definitely got some wires crossed somewhere. Perhaps someone could lend a hand to relieve some of my frustration?

EDIT some code to show what I'm doing

The controller is loaded when my application finishes launching, from the app delegate:

MyController *controller = [[MyController alloc] initWithNibName:@"MyController" bundle:nil];
[window setContentView:[controller view]];

And then in my initWithNibName I don't do anything but call to super for now.

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

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

发布评论

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

评论(2

帅哥哥的热头脑 2024-08-18 04:56:28

将每个视图分解为自己的笔尖并使用 NSViewController 时,典型的处理方法是为每个笔尖创建一个 NSViewController 子类。然后,每个相应 nib 文件的文件所有者将被设置为该 NSViewController 子类,并且您将 view 出口连接到 nib 中的自定义视图。然后,在控制主窗口内容视图的视图控制器中,实例化每个 NSViewController 子类的实例,然后将该控制器的视图添加到您的窗口中。

一段快速的代码 - 在此代码中,我调用主要内容视图控制器 MainViewController,“工具栏”的控制器是 TopViewController,其余部分内容是ContentViewController

//MainViewController.h
@interface MainViewController : NSViewController
{
    //These would just be custom views included in the main nib file that serve
    //as placeholders for where to insert the views coming from other nibs
    IBOutlet NSView* topView;
    IBOutlet NSView* contentView;
    TopViewController* topViewController;
    ContentViewController* contentViewController;
}

@end

//MainViewController.m
@implementation MainViewController

//loadView is declared in NSViewController, but awakeFromNib would work also
//this is preferred to doing things in initWithNibName:bundle: because
//views are loaded lazily, so you don't need to go loading the other nibs
//until your own nib has actually been loaded.
- (void)loadView
{
    [super loadView];
    topViewController = [[TopViewController alloc] initWithNibName:@"TopView" bundle:nil];
    [[topViewController view] setFrame:[topView frame]];
    [[self view] replaceSubview:topView with:[topViewController view]];
    contentViewController = [[ContentViewController alloc] initWithNibName:@"ContentView" bundle:nil];
    [[contentViewController view] setFrame:[contentView frame]];
    [[self view] replaceSubview:contentView with:[contentViewController view]];
}

@end

When breaking out each view into its own nib and using NSViewController, the typical way of handling things is to create an NSViewController subclass for each of your nibs. The File's Owner for each respective nib file would then be set to that NSViewController subclass, and you would hook up the view outlet to your custom view in the nib. Then, in the view controller that controls the main window content view, you instantiate an instance of each NSViewController subclass, then add that controller's view to your window.

A quick bit of code - in this code, I'm calling the main content view controller MainViewController, the controller for the "toolbar" is TopViewController, and the rest of the content is ContentViewController

//MainViewController.h
@interface MainViewController : NSViewController
{
    //These would just be custom views included in the main nib file that serve
    //as placeholders for where to insert the views coming from other nibs
    IBOutlet NSView* topView;
    IBOutlet NSView* contentView;
    TopViewController* topViewController;
    ContentViewController* contentViewController;
}

@end

//MainViewController.m
@implementation MainViewController

//loadView is declared in NSViewController, but awakeFromNib would work also
//this is preferred to doing things in initWithNibName:bundle: because
//views are loaded lazily, so you don't need to go loading the other nibs
//until your own nib has actually been loaded.
- (void)loadView
{
    [super loadView];
    topViewController = [[TopViewController alloc] initWithNibName:@"TopView" bundle:nil];
    [[topViewController view] setFrame:[topView frame]];
    [[self view] replaceSubview:topView with:[topViewController view]];
    contentViewController = [[ContentViewController alloc] initWithNibName:@"ContentView" bundle:nil];
    [[contentViewController view] setFrame:[contentView frame]];
    [[self view] replaceSubview:contentView with:[contentViewController view]];
}

@end
千仐 2024-08-18 04:56:28

MainViewController 不应该是 NSWindowController 的子类吗?类中的插座连接到 MainMenu.xib 中主窗口中的视图元素?
让我们希望旧的线程仍然被阅读......

Should not MainViewController be a subclass of NSWindowController? And the outlets in the class connected to view elements in the main Window in MainMenu.xib?
Let's hope old threads are still read...

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