为什么这两个init函数被调用
如果我有一个像这样设置的类来自定义一个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 }
不pipe我用的是怎么两次叫foo
myView *aView = [[myView alloc]init]];
要么
myView *aView = [[myView alloc]initWithFram:aFrame];
UIView init
调用UIView initWithFrame:
既然你重写了两者,调用你的init
方法会导致你的initWithFrame:
方法被调用:
换句话说:你的init
调用UIView init
。 UIView init
调用initWithFrame:
既然你重写了initWithFrame:
你的initWithFrame:
被调用,然后调用UIView initWithFrame:
解决scheme,因为你的initWithFrame:
总是被调用,就是从你的init
方法中去掉对foo
的调用。