objective-c - 向 MainWindow 添加 View 或窗口

标签 objective-c macos cocoa

我在一些我无法掌握的基本概念上磕磕绊绊。我希望有人可以为我解决一些问题,因为我没有找到可以解释这一点的资源。或者,它在明亮的地方,我只是看不到它。

到目前为止的理解:MainWindow 包含菜单,因此或多或少是必不可少的。 info.plist 包含在 appstart 上加载的 Nib 。

目前还不明白:我正在尝试单窗口应用程序。我可以在 appDelegate 中做所有事情(到目前为止工作正常)并且我可以有一个额外的 Controller ,我可以将 UIElements 从 MainWindow 连接到(虽然到目前为止工作正常)。但是:我真正想做的是有一个 MainWIndow,它只有菜单,以及一个单独的 Controller 和 Nib (以后可能甚至不止一个),随后加载和添加。

我的问题:

  • 使用 NSWindowController 还是 NSViewController?为什么? (我会使用 NSViewController)

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

  • 如何将窗口或 View 添加到唯一的主窗口而不是第二个独立窗口(我还没有准备好使用 multiDocumentApps)

非常感谢,任何想法都值得赞赏!

最佳答案

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.

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

默认情况下,Xcode 会创建一个包含主菜单和主窗口的 MainWindow.nib 文件。您可以随意从 MainWindow.nib 文件中删除该窗口,并使用另一个 nib 文件来保存该窗口。我们将此另一个 nib 文件称为 MyWindow.nib。

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

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

执行此操作时,请务必在 MyWindow.nib 中执行以下操作:

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

  • 将文件所有者中的 window 导出连接到 window 对象。

NSViewController 用于管理 View ,一般从nib文件加载,不适用于windows。

您可以重复此操作 — .nib 文件中的窗口,NSWindowController 的子类来管理该窗口 — 所需数量的窗口。

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

由于您希望在您的应用程序中使用单个窗口,因此您的应用程序委托(delegate)可以选择一个选项来保存对管理该窗口的单个窗口 Controller 的引用(实例变量、声明的属性)。然后,在 -applicationDidFinishLaunching: 中,实例化所述 Controller 。

例如:

// MyAppDelegate.h

@class MyWindowController;

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

和:

// 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

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

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)

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

例如,考虑到 MyWindowController 在 MyWindowController.m 中有一个声明的属性 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];
    }
}

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

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

[self.window.contentView addSubView:someView];

您可以通过编程方式创建 someView 或从单独的 nib 文件加载它。在后一种情况下,过程与上面描述的非常相似,但不是使用 NSWindowController 的子类,而是使用 NSViewController 的子类。

例如,在 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];
    }
}

关于objective-c - 向 MainWindow 添加 View 或窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7511619/

相关文章:

ios - 在 iOS 中显示隐藏 View

objective-c - 在 Cocoa 中使用自定义模式表时遇到问题

macos - 在 Mac 上开始使用 cronjobs

cocoa - 粘贴板上的多种类型,包括文件、rtfd 和自定义类型 (Mac OS X 10.6)

objective-c - 无法访问的对象在变得无法访问后是否随时都可以安全地收集?

iphone - 使用 AVAssetWriter 进行视频编码 - CRASHES

iphone - 找出 iPhone 有多少内存?

mysql 服务器未关闭并出现错误 : 'the server quit without updating pid file'

cocoa - 如何关闭 NSSecureTextField 中的项目符号?

ios - 将可拖动的 UIView 限制为 SuperView 的边界