我注意到当我在UIView
的子类中覆盖init
和initWithFrame:
时,两种方法都被调用。尽管我的代码只明确调用了其中一个方法:
TestViewController.m:
@implementation TestViewController
- (void)viewDidLoad
{
[super viewDidLoad];
View1 *view1 = [[View1 alloc] init];
[self.view addSubview:view1];
}
@end
View1.m:
@implementation View1
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
NSLog(@"initWithFrame");
}
return self;
}
- (id)init
{
self = [super init];
if (self)
{
NSLog(@"init");
}
return self;
}
@end
控制台的输出如下:
2013-10-17 12:33:46.209 test1[8422:60b] initWithFrame
2013-10-17 12:33:46.211 test1[8422:60b] init
为什么在调用init之前要先调用initWithFrame方法?