我创建了一个XIB
。
我创建了一个名为MyCustomView
的类,并将其分配给XIB
的文件所有者。
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) [self load];
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) [self load];
return self;
}
- (void)load {
NSArray* topLevelObjects;
[[NSBundle mainBundle] loadNibNamed:@"MyCustomView"
owner:self
topLevelObjects:&topLevelObjects];
NSView* view;
for (id aView in topLevelObjects) {
if ([umaVista isKindOfClass:[NSView class]]) {
view = umaVista;
break;
}
}
[self addSubview:view];
view.frame = self.bounds;
}
我在主应用上创建了NSView
。
我将该视图更改为MyCustomView
。
我运行应用程序。 MyCustomView
的{{1}}没有运行。 initWithCoder
无法运行。 initWithFrame
无法运行。
什么都没发生。
有什么想法吗?
答案 0 :(得分:1)
"文件所有者" as I've written elsewhere,不是档案中的真实对象。它是一个占位符。解压缩nib时,会使用预先存在的对象。
看起来您应该将自定义视图的实例放入nib中。不要将其作为文件所有者,只需从对象选项板中拖出一个视图,然后在Identity Inspector中更改其类(右侧窗格,顶部;按⌘-⌥-3)。在笔尖中构建子视图。
然后让你的NSBundle
为你加载笔尖。您的自定义视图将获得initWithCoder:
和awakeFromNib
,您不必在层次结构中浏览以查找特定的子视图。