macos 向 MainWindow 添加视图或窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7511619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 08:27:40  来源:igfitidea点击:

Adding views or windows to MainWindow

objective-cmacoscocoa

提问by Marcus Toepper

I'm stumbling over some basic concepts, that I cannot grasp. I hope, somebody can clear some things up for me, as I've found no resource, that would explain that. OR maybe, it's in the bright open and I just don't see it.

我绊倒了一些我无法理解的基本概念。我希望,有人可以为我清理一些事情,因为我找不到资源,这可以解释这一点。或者也许,它在明亮的开放中,我只是没有看到它。

Understood so far: The MainWindow holds the menu and is therefore more or less essential. The info.plistholds the nib, that's loaded on appstart.

到目前为止已理解: MainWindow 包含菜单,因此或多或少是必不可少的。该info.plist持有笔尖,这是装上AppStart的。

Not understood so far: I am trying a single window application. I can do everything in the appDelegate (works fine so far) and I can have an additional controller, to whcih I can connect UIElementsfrom the MainWindow to (works although fine so far). BUT: What I'd really like to do, is have a MainWIndow, that only has the menu, and a separate controller and nib (possibly even more than one of both later on), that are loaded and added subsequently.

到目前为止还没有理解:我正在尝试一个单窗口应用程序。我可以在 appDelegate 中做所有事情(到目前为止工作正常)并且我可以有一个额外的控制器,我可以UIElements从 MainWindow连接到它(尽管到目前为止工作正常)。但是:我真正想做的是有一个 MainWIndow,它只有菜单,以及一个单独的控制器和笔尖(以后可能甚至不止一个),它们随后被加载和添加。

My questions:

我的问题:

  • Use NSWindowControlleror NSViewController? And why? (I'd use NSViewController)

  • What, where and how to instantiate (presumably in the didFinishLaunchingof the appDelegate?)

  • How to add window or view to the one and only main-window instead of having a second, independent window (I'm not yet up for multiDocumentApps)

  • 使用NSWindowControllerNSViewController? 为什么?(我会用NSViewController

  • 实例化什么、在哪里以及如何实例化(大概在didFinishLaunchingappDelegate 中?)

  • 如何向一个且唯一的主窗口添加窗口或视图,而不是拥有第二个独立窗口(我还没有准备好使用 multiDocumentApps)

Thanx a lot, any thoughts appreciated!

非常感谢,任何想法表示赞赏!

回答by

Not understood so far: I am trying a single window application. I can do everything in the appDelegate (works fine so far) and I can have an additional controller, to whcih I can connect UIElements from the MainWindow to (works although fine so far). BUT: What I'd really like to do, is have a MainWIndow, that only has the menu, and a separate controller and nib (possibly even more than one of both later on), that are loaded and added subsequently.

到目前为止还没有理解:我正在尝试一个单窗口应用程序。我可以在 appDelegate 中做所有事情(到目前为止工作正常)并且我可以有一个额外的控制器,我可以将 UIElements 从 MainWindow 连接到(尽管到目前为止工作正常)。但是:我真正想做的是有一个 MainWIndow,它只有菜单,以及一个单独的控制器和笔尖(以后可能甚至不止一个),它们随后被加载和添加。

For the sake of clarity, MainWindow is a nib file so I'll refer to it as MainWindow.nib. It is the standard nib file name specified in the application's Info.plist file as the nib file to be loaded when the application starts.

为清楚起见,MainWindow 是一个 nib 文件,因此我将其称为 MainWindow.nib。它是应用程序的 Info.plist 文件中指定的标准 nib 文件名,作为应用程序启动时要加载的 nib 文件。

By default, Xcode creates a MainWindow.nib file that contains both the main menu and the main window. You're free to delete the window from MainWindow.nib file and have another nib file to hold that window. Let's call this other nib file MyWindow.nib.

默认情况下,Xcode 创建一个 MainWindow.nib 文件,其中包含主菜单和主窗口。您可以自由地从 MainWindow.nib 文件中删除窗口,并使用另一个 nib 文件来保存该窗口。让我们将这个其他笔尖文件称为 MyWindow.nib。

Use NSWindowController or NSViewController? And why? (I'd tend to viewController)

使用 NSWindowController 还是 NSViewController?为什么?(我倾向于 viewController)

Since you'll have a separate nib file to hold a window, you'll use NSWindowController. Create a subclass of NSWindowController, e.g. MyWindowController, which will be responsible for controlling the window stored in MyWindow.nib. This subclass will have outlets pointing to the user-interface elements in that window, and potentially other objects you define in MyWindow.nib.

由于您将有一个单独的 nib 文件来保存window,因此您将使用NSWindowController. 创建一个子类NSWindowController,例如MyWindowController,它将负责控制存储在 MyWindow.nib 中的窗口。该子类将具有指向该窗口中的用户界面元素的出口,以及您在 MyWindow.nib 中定义的潜在其他对象。

When doing this, it's important that you do the following in MyWindow.nib:

执行此操作时,在 MyWindow.nib 中执行以下操作很重要:

  • Set the file's owner to be of type MyWindowController;

  • Connect the windowoutlet in file's owner to the window object.

  • 将文件的所有者设置为类型MyWindowController

  • window文件所有者中的插座连接到窗口对象。

NSViewControlleris used to manage a view, normally loaded from a nib file, and it doesn't apply to windows.

NSViewController用于管理视图,通常从 nib 文件加载,不适用于 windows。

You can repeat this — window in a .nib file, subclass of NSWindowControllerto manage that window — for as many windows as needed.

您可以重复此操作 — .nib 文件NSWindowController中的窗口,管理该窗口的子类— 根据需要为多个窗口。

What, where and how to instantiate (presumably in the didFinishLaunching of the appDelegate?)

什么、在哪里以及如何实例化(大概在 appDelegate 的 didFinishLaunching 中?)

Since you want a single window in your application, one option is for your application delegate to hold a reference (instance variable, declared property) to the single window controller that manages that window. Then, in -applicationDidFinishLaunching:, instantiate said controller.

由于您希望应用程序中只有一个窗口,因此一种选择是让您的应用程序委托保存对管理该窗口的单个窗口控制器的引用(实例变量、声明的属性)。然后,在 中-applicationDidFinishLaunching:,实例化所述控制器。

For example:

例如:

// MyAppDelegate.h

@class MyWindowController;

@interface MyAppDelegate : NSObject <NSApplicationDelegate>
@property (retain) MyWindowController *myWindowController;
@end

and:

和:

// MyAppDelegate.m

#import "MyAppDelegate.h"
#import "MyWindowController.h"

@implementation MyAppDelegate

@synthesize myWindowController;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    self.myWindowController = [[[MyWindowController alloc]
        initWithWindowNibName:@"MyWindow"] autorelease];
    [self.myWindowController showWindow:self];
}

…
@end

If you want more windows, it's just a matter of declaring instance variables/properties to hold their corresponding controllers and instantiate the controllers like above.

如果你想要更多的窗口,只需声明实例变量/属性来保存它们相应的控制器并像上面那样实例化控制器。

How to add window or view to the one and only main-window instead of having a second, independent window (I'm not yet up for multiDocumentApps)

如何向一个且唯一的主窗口添加窗口或视图,而不是拥有第二个独立窗口(我还没有准备好使用 multiDocumentApps)

Are you sure you want to add a windowto the main window? If so, that'd be called an attached window. You can use the mechanism above (place the window in its own nib file, have a subclass of NSWindowControllerto manage it, have your original MyWindowControllerhold a reference (instance variable, declared property) to the attached window controller, and instantiate it/load the attached window nib file when appropriate) and then use -[NSWindow - addChildWindow:ordered:]to attach the secondary window to the main window.

你确定你要添加一个窗口到主窗口?如果是这样,那将被称为附加窗口。您可以使用上面的机制(将窗口放在自己的 nib 文件中,有一个子类NSWindowController来管理它,让您的原始文件MyWindowController持有对附加窗口控制器的引用(实例变量,声明的属性),并实例化它/加载附加的window nib 文件(在适当时),然后用于-[NSWindow - addChildWindow:ordered:]将辅助窗口附加到主窗口。

For example, considering MyWindowControllerhas a declared property secondaryWindowController, in MyWindowController.m:

例如,考虑在 MyWindowController.m 中MyWindowController有一个声明的属性secondaryWindowController

- (void)someAction:(id)sender {
    // If the secondary window hasn't been loaded yet, load it and add its
    // window as a window attached to the main window
    if (self.secondaryWindowController == nil) {
        self.secondaryWindowController = [[[MySecondaryWindowController alloc]
            initWithWindowNibName:@"MySecondaryWindow"] autorelease];
        [[self window] addChildWindow:self.secondaryWindowController.window
                              ordered:NSWindowAbove];
    }
}

If you want to add a view to the main window, the easiest way is to do so in the nib file directly.

如果要向主窗口添加视图,最简单的方法是直接在 nib 文件中进行。

If you need/want to do it programatically, you need to have a reference to the view and then add it to the main window's content view, e.g.:

如果您需要/想要以编程方式执行此操作,则需要对视图进行引用,然后将其添加到主窗口的内容视图中,例如:

[self.window.contentView addSubView:someView];

You can create someViewprogrammatically or load it from a separate nib file. In the latter case, the procedure is much like what was described above but instead of using a subclass of NSWindowControlleryou'd use a subclass of NSViewController.

您可以通过someView编程方式创建或从单独的 nib 文件中加载它。在后一种情况下,过程更象是上面,但不是使用的一个亚类说明NSWindowController你会使用的子类NSViewController

For example, in MyWindowController.m:

例如,在 MyWindowController.m 中:

- (void)anotherAction:(id)sender {
    // If the view hasn't been loaded yet, load it and add it
    // as a subview of the main window's content view
    if (self.someViewController == nil) {
        self.someViewController = [[[MyViewController alloc]
            initWithNibName:@"MyView" bundle:nil] autorelease];
        // You'll probably want to set the view's frame, e.g.
        // [self.someViewController.view setFrame:...];
        [self.window.contentView addSubview:self.someViewController.view];
    }
}