gpt4 book ai didi

objective-c - 加载后指向 NSWindow Xib 的指针?

转载 作者:太空狗 更新时间:2023-10-30 03:46:58 34 4
gpt4 key购买 nike

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

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

加载 XIB 后如何获得一个指针来控制它,以便您可以执行诸如居中 NSWindow(我的意思是驻留在 XIB 内部的序列化的)之类的事情?

我做错了什么?

最佳答案

你应该使用 NSWindowController子类。 NSWindowController专门设计用于完成您想要实现的目标,并解决了如果您使用 NSBundle 的方法直接加载 Nib 时会遇到的几个问题。 .您通常应该始终使用 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并连接 window File's Owner 的导出到您的 Nib 中的窗口对象。

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

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

关于objective-c - 加载后指向 NSWindow Xib 的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2427889/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com