从 Cocoa 中的其他 nib(即 Interface Builder)加载 nib

发布于 2024-12-22 17:44:09 字数 1488 浏览 2 评论 0原文

我在 Interface Builder 中有一个视图设置,其中包含一个加载另一个 Nib 文件的 viewController。但是,由于某种原因,正在加载的 nib 文件中包含的对象永远不会被调用 awakeFromNib。我在这里缺少什么?无论如何,是否可以在界面生成器中从 Nib 加载视图,并在界面生成器中管理它们的属性和大小?

在此处输入图像描述

一般来说,管理多个笔尖并将它们组合成复杂视图的最佳实践是什么?

最终解决方案:

我创建了一个像这样的 NSViewController 子类。

@interface NibLoadingViewController : NSViewController

// The placeholder would be replaced during run-time by the view
@property (weak, nonatomic) IBOutlet NSView *placeholder;

@end

@implementation NibLoadingViewController 

@synthesize placeholder = _placeholder;

- (void)awakeFromNib {
    if (self.placeholder)
        self.view = self.view; // Trigger lazy loading
}

- (void)loadView {
    [super loadView];
    if (!self.view)
        return;
    // Replace the placehoder if it exists
    if (self.placeholder) {
        // Copy over relevant attributes
        self.view.frame = self.placeholder.frame;
        self.view.autoresizingMask = self.placeholder.autoresizingMask;
        self.view.autoresizesSubviews = self.placeholder.autoresizesSubviews;
        // Replaces the old view
        [self.placeholder.superview replaceSubview:self.placeholder with:self.view];
        self.placeholder = nil;
    }
    self.nextResponder = self.view.nextResponder;
    self.view.nextResponder = self;
}

@end

这样,您只需将占位符出口挂接到包含视图控制器的笔尖中,它就会自动为您加载其他笔尖,并将占位符中的所有属性复制到主笔尖中并将其替换。

I have a view setup in Interface Builder which contains a viewController that loads another Nib file. However, for some reason the objects contained in the nib file being loaded never gets called awakeFromNib. What am I missing here? Is there anyway to load views from Nib in interface builder and also manage their properties and sizing in the interface builder?

enter image description here

In general, what are the best practices for managing multiple nibs and composing them into complex views?

Final Solution:

I created a NSViewController subclass like this.

@interface NibLoadingViewController : NSViewController

// The placeholder would be replaced during run-time by the view
@property (weak, nonatomic) IBOutlet NSView *placeholder;

@end

@implementation NibLoadingViewController 

@synthesize placeholder = _placeholder;

- (void)awakeFromNib {
    if (self.placeholder)
        self.view = self.view; // Trigger lazy loading
}

- (void)loadView {
    [super loadView];
    if (!self.view)
        return;
    // Replace the placehoder if it exists
    if (self.placeholder) {
        // Copy over relevant attributes
        self.view.frame = self.placeholder.frame;
        self.view.autoresizingMask = self.placeholder.autoresizingMask;
        self.view.autoresizesSubviews = self.placeholder.autoresizesSubviews;
        // Replaces the old view
        [self.placeholder.superview replaceSubview:self.placeholder with:self.view];
        self.placeholder = nil;
    }
    self.nextResponder = self.view.nextResponder;
    self.view.nextResponder = self;
}

@end

This way, you just need to hook the placeholder outlet in the nib that contains the view controller and it will automatically load the other nibs for you and copy all the attributes from placeholder over and replace it in the main nib.

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

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

发布评论

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

评论(1

櫻之舞 2024-12-29 17:44:09

nib 文件的内容是延迟加载的。如果您希望调用 -(void)awakeFromNib,则需要首先从 nib 文件中访问某些内容。

NSViewController *controller = [[NSViewController alloc] initWithNibName:@"MyView" bundle:nil];

/*
 * awakeFromNib was not called yet
 */

NSView *view = controller.view;

/*
 * but now -(void)awakeFromNib was called.
 */

The content of the nib-file is lazy-loaded. If you want -(void)awakeFromNib to be called, you need to access something from the nib-file first.

NSViewController *controller = [[NSViewController alloc] initWithNibName:@"MyView" bundle:nil];

/*
 * awakeFromNib was not called yet
 */

NSView *view = controller.view;

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