The documentation for viewWillAppear:
isn't completely clear but have a look at what it says:
This method is called in response to a
view being added either directly or
indirectly to a window. You add views
to a window directly by using the
addSubview: method to add them to the
window’s view hierarchy. You can also
add a view indirectly in several ways,
including by pushing it onto a
navigation stack or by presenting it
modally (using the
presentModalViewController:animated:
method). This method is called before
the view is actually added to the
window and before any animations are
configured.
The viewDidAppear:
and viewWillAppear:
methods will only be called if you are:
- Adding them to a window (adding to a view does not count -- it must be a window)
- Using
pushViewController:
or presentModalViewController:animated:
or switching tabs in a tab controller or any other situation where an Apple-provided view controller adds the view controller and its view for you.
In the situation you have, where you are adding a view to another view, you must invoke viewWillAppear:
and viewDidAppear:
on the view controller yourself or they won't happen.
I don't know why viewDidLoad
was not getting invoked for you initially. It will always be invoked after loadView
, when the view
property accessor is invoked on the controller for the first time, unless an error occurs. It is possible the view load was failing (you'd have to check the console log).
You do not need to follow addSubview:
with bringSubviewToFront:
since addSubview:
always adds to the front. Again, from the documentation of addSubview:
Adds a view to the receiver’s subviews
so it’s displayed above its siblings.