Luca Luca - 1 year ago 104
Objective-C Question

iOS: UIView subclass init will call [super init] then call methods in super class, why it will call [subclass initWithFrame:xx]?

I am confused with [UIView init] and [UIView initWithFrame:xx], after i search the stackoverflow and i found below questions and answers´╝Ü

Why do both init functions get called

iOS: UIView subclass init or initWithFrame:?
then i konw the initwithFrame is the designed initializer, what make me confused with the answer is when we call [myview init](myView is subclass of UIView and overwrite init and initwithfrme:), it will call call [super init] then it will call [super initWithFrame:xx] as super will find methods in super class, why it will call [myView initWithFrame:xx]???

Answer Source

Since initWithFrame: is the designated initializer, apple's implementation of init (which you call when you call [super init]) internally calls the initWithFrame: function and passes in CGRectZero. That is the reason both get called. So the end flow ends up looking like this:

[YourClass init] -> [super init] -> [self initWithFrame:CGRectZero] -> 
[YourClass initWithFrame:CGRectZero] -> [super initWithFrame:CGRectZero]

This is assuming you call [super init] when you override init in YourClass and [super initWithFrame:] when you override initWithFrame.