[英]How to correctly load and show custom UIView in XIB?
任务很简单:
UPD:任务已完成。 我们应该使用initWithCoder。 Pizzaiola Gorgonzola引用:«initWithFrame是通过代码动态创建UIView时调用的。 从.nib文件加载的视图始终使用initWithCoder实例化,编码器负责从.nib文件中读取设置»。 多亏了问题Subviews没有出现在UIView类中 。
如果这些元素不在nib / xib中,则可以在initWithCoder中添加/创建它们:
您可以执行以下操作:
//MYCustomView.h
@interface MYCustomView : UIView
@end
//MYCustomView.m
@implementation MYCustomView
- (void)setup {
// Do custom stuffs here...
}
- (id)init {
self = [super init];
if (self) {
[self setup];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
@end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.