创建没有nib的自定义NSViewController [英] Creating a custom NSViewController without a nib

查看:701
本文介绍了创建没有nib的自定义NSViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习Objective-C,我遇到了一个小问题。我试图使用没有nib的自定义视图控制器,因此视图是在代码中创建的。视图控制器本身是在AppDelegate中创建的。

I recently started learning Objective-C and I've run into a slight problem. I'm trying to use a custom view controller without a nib, so the view is created in the code. The view controller itself is created in the AppDelegate.

当我运行程序时,它首先显示一个默认的空窗口。在我关闭这个窗口后,弹出一个第二个窗口,它正确地包含了视图。我显然不想让第一个窗口出现,但我不知道是什么原因。我可以找到关于这个主题的唯一的信息是iOS开发,这是不一样的。

When I run the program, it first displays a default empty window. After I close this window, a second window pops up which correctly contains the view. I obviously don't want that first window to appear, but I don't know what causes it. The only information I could find on this subject was for iOS development, which isn't quite the same.

我也收到这个消息,我不知道它的含义:无法连接操作orderFrontStandardAboutPanel:到MainViewController类的目标

I also get this message of which I'm not really sure what it means: Could not connect the action orderFrontStandardAboutPanel: to target of class MainViewController

AppDelegate:

AppDelegate:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    mainViewController = [[MainViewController alloc] initWithFrame:_window.frame];
    _window.contentView = mainViewController.view;
}

MainViewController:

MainViewController:

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithNibName:nil bundle:nil];
    if (self)
    {
        [self setView:[[MainView alloc] initWithFrame:frame]];
        [self loadView];
    }
    return self;
}


推荐答案

重写 loadView 并将 self.view 设置为您的视图。

In short, override loadView and set self.view to your view. You'll want to give it a frame before you set it.

loadView 的默认实现是在其中尝试加载笔尖。如果你不想这样做,你打算覆盖这个。

The default implementation of loadView is where it tries to load the nib. You're intended to override this if you don't want that.

这篇关于创建没有nib的自定义NSViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆