从 Cocoa 中的其他 nib(即 Interface Builder)加载 nib
我在 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?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
nib 文件的内容是延迟加载的。如果您希望调用 -(void)awakeFromNib,则需要首先从 nib 文件中访问某些内容。
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.