加载后指向 NSWindow Xib 的指针?

发布于 2024-08-25 07:51:36 字数 310 浏览 4 评论 0原文

在我下面的代码中,CustomWindow 是 NSWindow 的子类。

CustomWindow *window = [[CustomWindow alloc] init];
if (![NSBundle loadNibNamed:@"NibName" owner:window])
[window center]; // doesn't work

加载 XIB 后,如何获得一个指针来控制 XIB,以便可以执行诸如将 NSWindow 居中(我的意思是驻留在 XIB 内部的序列化窗口)等操作?

我在这里做错了什么?

In my code below, CustomWindow is a subclass of NSWindow.

CustomWindow *window = [[CustomWindow alloc] init];
if (![NSBundle loadNibNamed:@"NibName" owner:window])
[window center]; // doesn't work

How do you get a pointer to control your XIB after you load it so you can do things such as centering the NSWindow (I mean the serialised one that resides inside the XIB)?

What am i doing wrong here?

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

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

发布评论

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

评论(4

雨夜星沙 2024-09-01 07:51:36

您应该使用 NSWindowController 子类。 NSWindowController 专门设计用于完成您想要实现的目标,并解决了使用 NSBundle 的方法直接加载笔尖时会遇到的几个问题。通常,您应该始终使用 NSWindowController 子类来管理窗口。

创建NSWindowController的子类:

@interface MyWindowController : NSWindowController {}
@end

@implementation MyWindowController
- (id)init
{
    self = [super initWithWindowNibName:@"MyWindow"];
    if(self)
    {
        //initialize stuff
    }
    return self;
}
//this is a simple override of -showWindow: to ensure the window is always centered
-(IBAction)showWindow:(id)sender
{
    [super showWindow:sender];
    [[self window] center];
}
@end

在Interface Builder中,将File's Owner的类设置为MyWindowController并连接窗口文件所有者出口到笔尖中的窗口对象。

然后您可以通过执行以下操作来显示该窗口:

MyWindowController* controller = [[MyWindowController alloc] init];
[controller showWindow:self];

You should be using an NSWindowController subclass. NSWindowController is specifically designed to do exactly what you want to achieve and solves several problems that you will run into if you load the nib directly using the methods of NSBundle. You generally should always use an NSWindowController subclass to manage windows.

Create a subclass of NSWindowController:

@interface MyWindowController : NSWindowController {}
@end

@implementation MyWindowController
- (id)init
{
    self = [super initWithWindowNibName:@"MyWindow"];
    if(self)
    {
        //initialize stuff
    }
    return self;
}
//this is a simple override of -showWindow: to ensure the window is always centered
-(IBAction)showWindow:(id)sender
{
    [super showWindow:sender];
    [[self window] center];
}
@end

In Interface Builder, set the class of File's Owner to be MyWindowController and connect the window outlet of File's Owner to the window object in your nib.

You can then display the window by doing this:

MyWindowController* controller = [[MyWindowController alloc] init];
[controller showWindow:self];
凤舞天涯 2024-09-01 07:51:36

在我下面的代码中,CustomWindow 是 NSWindow 的子类。

CustomWindow *window = [[CustomWindow alloc] init];
if (![NSBundle loadNibNamed:@"NibName" 所有者:window])
[窗口中心]; // 不起作用

加载 XIB 后,如何获得一个指针来控制它,以便可以执行诸如将 NSWindow 置于 XIB 内中心等操作?

“将 NSWindow 在 XIB 中居中”没有任何意义(您会将其在屏幕上居中),除非您的意思是将 XIB 中的 NSWindow 对象居中,在这种情况下,为什么要创建另一个xib 之外的 NSWindow (CustomWindow) 对象?

请记住,nib(或 xib)是对象的存档。如果您想使用笔尖中的窗口,则需要创建一个指向该窗口的插座,将文件所有者的类设置为您添加插座的类,将插座挂接到IB,并将具有出口的对象指定为文件所有者,方法是将其传递给 owner: 参数。该对象作为所有者,将负责处理窗口。它可能(通常是,在我的代码中)与加载笔尖的对象相同。

另外,init 不适用于 NSWindow;您必须使用 initWithContentRect:styleMask:backing:defer:initWithContentRect:styleMask:backing:defer:screen:。仅当您自己在 CustomWindow 中实现了 init 并将这两个选择器之一用于 [super init...]init 才有效> 消息。

In my code below, CustomWindow is a subclass of NSWindow.

CustomWindow *window = [[CustomWindow alloc] init];
if (![NSBundle loadNibNamed:@"NibName" owner:window])
[window center]; // doesn't work

How do you get a pointer to control your XIB after you load it so you can do things such as centering the NSWindow inside the XIB?

“centering the NSWindow inside the XIB” makes no sense (you would center it on the screen), unless you mean centering the NSWindow object that is inside the xib, in which case, why are you creating another NSWindow (CustomWindow) object outside of the xib?

Remember that a nib (or xib) is an archive of objects. If you want to use a window that you have in your nib, you need to create an outlet to point to that window, set the class of the File's Owner to be the class where you've added the outlet, hook up the outlet in IB, and appoint the object with the outlet as the File's Owner by passing it to the owner: argument. That object, as the owner, will then be responsible for working with the window. It may be (usually is, in my code) the same object that loads the nib.

Also, init doesn't work on NSWindow; you must use initWithContentRect:styleMask:backing:defer: or initWithContentRect:styleMask:backing:defer:screen:. Using init would only be valid if you've implemented init yourself in CustomWindow, and used one of those two selectors for the [super init…] message.

奢望 2024-09-01 07:51:36

您可能不想让您的窗口成为文件的所有者。通常你会在那里传递 self 或一些控制器对象。然后,如果 self 或该控制器对象具有 CustomWindow IBOutlet,则当您调用 loadNibNamed: 时,它将被连接。查看这篇文章获取示例代码。

You probably don't want to make your window the File's Owner. Normally you would pass self or some controller object there. Then if self or that controller object has a CustomWindow IBOutlet, it will get hooked up when you call loadNibNamed:. Check out this post for example code.

蘸点软妹酱 2024-09-01 07:51:36

XIB 是对象的容器,它不等于窗口。您无法将 XIB 居中,只能将 XIB 中包含的窗口居中。

此外,XIB 中的对象是在加载时创建的。您不会将对象作为所有者传递,然后代表 XIB 中的对象之一,而是使用 IBOutlet 来获取对加载 XIB 时创建的新对象的引用,然后可以与它们进行交互。

文件的所有者对象是 XIB 中的特殊对象,因为它是唯一未创建的对象,您可以通过将其传递给 loadNibNamed:owner: 来指定。它是 XIB 创建的对象和您的应用程序之间的网关。

通常,所有者对象是某种控制器类。将 Interface Builder 中的 File's Owner's 类设置为您的控制器类,然后在类中定义一些 IBOutlet,它们将显示在 Interface Builder 中的 File's Owner 上,您可以将 XIB 中的对象连接到它们。

最后,当您将控制器对象传递给 loadNibNamed:owner: 时,Cocoa 会将您的 IBOutlet 连接到新创建的对象,您可以使用它们与它们进行交互,例如在 XIB 中将窗口居中。

A XIB is a container for objects, it's not equal to a window. You can't center a XIB, you can only center a window contained in a XIB.

Also, the objects in the XIB are created when you load it. You don't pass an object as owner that then stands in for one of the objects in the XIB, you instead use IBOutlets to get references to the new objects created when loading the XIB and then you can interact with them.

The File's Owner object is a special object in XIBs, as it's the only object that is not created and that you can specify by passing it to loadNibNamed:owner:. It's your gateway between the XIB-created objects and your application.

Usually, the owner object is some kind of controller class. Set the File's Owner's class in Interface Builder to your controller class, then define some IBOutlets in the class, they will show up in Interface Builder on the File's Owner and you can connect your objects in the XIB to them.

Finally, when you pass your controller object to loadNibNamed:owner:, Cocoa will connect your IBOutlets to the newly created objects and you can use them to interact with them, e.g. to center a window in your XIB.

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