向MainWindow添加视图或窗口 [英] Adding views or windows to MainWindow

查看:192
本文介绍了向MainWindow添加视图或窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

了解到目前为止:MainWindow保存菜单,因此或多或少是必要的。 info.plist 保存在appstart上加载的nib。

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

到目前为止还不明白:单窗口应用。我可以做任何事情在appDelegate(工作正常到目前为止),我可以有一个额外的控制器,以便我可以连接 UIElements 从MainWindow到(工作虽然很好,到目前为止)。但是,我真正想做的是,有一个MainWIndow,只有菜单,一个单独的控制器和nib(可能甚至更多的两个),随后加载和添加。

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.

我的问题:


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

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

code> didFinishLaunching of the appDelegate?)

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

如何添加窗口或视图到唯一的主窗口而不是有一个第二个独立的窗口(我还没有为multiDocumentApps)

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)

Thanx很多,任何

Thanx a lot, any thoughts appreciated!

推荐答案


到目前为止还不明白:我在尝试一个窗口应用程序。我可以做所有的appDelegate(工作正常到目前为止),我可以有一个额外的控制器,我可以连接UIElements从MainWindow到(工作虽然很好,到目前为止)。但是,我真正想做的是,有一个MainWIndow,只有菜单,一个单独的控制器和nib(可能甚至更多的两个),随后加载和添加。

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文件。

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.

默认情况下,Xcode创建一个MainWindow。 nib文件,其中包含主菜单和主窗口。您可以从MainWindow.nib文件中自由删除该窗口,并有另一个nib文件来保存该窗口。让我们调用另一个nib文件MyWindow.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.


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

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

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

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.

这样做很重要,在MyWindow.nib中执行以下操作:

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


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

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

将档案拥有者的视窗 / p>

Connect the window outlet in file’s owner to the window object.

NSViewController 用于管理视图,通常从

NSViewController is used to manage a view, normally loaded from a nib file, and it doesn’t apply to windows.

您可以在.nib文件中重复此 - 窗口, NSWindowController 以管理该窗口 - 用于所需的窗口。

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


什么,在哪里和如何实例化appDelegate的didFinishLaunching?)

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

由于您想在应用程序中使用单个窗口,引用(实例变量,声明的属性)到管理该窗口的单个窗口控制器。然后,在 -applicationDidFinishLaunching:中,实例化所述控制器。

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.

例如:

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

你想要更多的窗口,只是声明实例变量/属性来保存他们相应的控制器和实例化控制器,如上所述。

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.


添加窗口或视图到一个和唯一的主窗口,而不是有一个第二,独立的窗口(我还没有为multiDocumentApps)

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 保存一个引用(实例变量,声明的属性)到附加的窗口控制器,并实例化它/加载附加的窗口nib文件,然后使用 - [NSWindow - addChildWindow:ordered :] 将辅助窗口附加到主窗口。

Are you sure you want to add a window to 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 NSWindowController to manage it, have your original MyWindowController hold 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.

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

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

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

如果要将视图添加到主窗口,最简单的方法是直接在nib文件中这样做。

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

如果你需要/想做它的程序,你需要一个引用视图,然后添加到主窗口的内容视图,例如:

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

您可以以编程方式创建 someView 从单独的nib文件。在后一种情况下,过程很像上面描述的,而不是使用 NSWindowController 的子类,您将使用 NSViewController

You can create someView programmatically 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 NSWindowController you’d use a subclass of NSViewController.

例如,在MyWindowController.m中:

For example, in 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];
    }
}

这篇关于向MainWindow添加视图或窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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