Please let me know at what times init and loadView
method gets called.
To my knowledge init method gets called only once when view is initialized and loadView
is called anytime view is loaded. So, even if you are pushing a new view in the view stack and then popping it then also the loadView
of the pop开发者_开发问答ed up view should get called. But when I am running my code in debugging mode, both of these methods are getting called once, irrespective of how many times I am loading the same screen. Please let me know if I am missing something.
you are right at some points :) The init method is being called when the ViewController object is instantiated. The loadView method gets called every time a ViewController should load its view into memory. This can happen before the view is displayed for the first time OR when it should be displayed for a second, third,... time but had been removed from memory before. (this might happen if your app runs out of memory.) If you want to execute some code every time the view becomes visible, you should have a look at the methods viewWillAppear/viewWillDisappear/viewDidAppear/viewDidDisappear.
loadView
is called when you access the view
property of your view controller and it's nil
.
If the view has been unloaded (viewDidUnload
has been called for memory purpose) then loadView
will be called again. If not it will not be called.
What you want is viewWillAppear:
or viewDidAppear:
.
精彩评论