如果我有一个类像这样设置来自定义UIView。
@interface myView : UIView
为什么当我使用
或者
@interface myView : UIView
- (id)init {
self = [super init];
if(self){
[self foo];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self foo];
}
return self;
}
-(void) foo{
//Build UIView here
}
为什么当我使用
foo
时它会被调用两次?myView *aView = [[myView alloc]init]];
或者
myView *aView = [[myView alloc]initWithFram:aFrame];
initWithFrame:
而不是init
。我认为init
调用[self initWithFrame:CGRectZero]
。 - Bryan Chen-[UIView init]
将调用指定的初始化方法-[UIView initWithFrame:]
。 OP希望保留对[myView foo]
的调用在initWithFrame:
中,并从init
中删除它。 可能要完全删除[myView init]
。 - Firoze Lafeer