Reputation: 27
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]???
Upvotes: 1
Views: 456
Reputation: 1787
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
.
Upvotes: 3
Reputation: 122439
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]???
No. There is no such thing is super
. super
is just a syntax to allow you to call a method on self
, using a different method lookup mechanism. Here, the [super init]
call will lookup to the method -[UIView init]
which is called on your object (the one self
points to). Inside -[UIView init]
, it has a call [self initWithFrame:]
, which again, is called on your object (the one self
points to). Here it does not use super
, so the normal method lookup mechanism is used (the superclass of UIView
doesn't have -initWithFrame:
anyway). The normal method lookup mechanism finds the most overridden implementation in the class of the object. Since your class (the class of your object) overrides -initWithFrame:
, the method it looks up to is -[YourClass initWithFrame:]
.
Upvotes: 0