Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why only viewWillAppear called on navigation back

I have doubt need to be clear.. I have stack and a navigation controller.now when the stack loads the viewDidLoad viewWillAppear viewDidAppear will be called. when i click some button then this button push me to the new stack , now new stack gives me the option of the back..now when i click on the back of the navigation controller..why only viewWillAppear will be called ..why not viewDidLoad and not viewDidAppear

like image 716
user2102546 Avatar asked Mar 11 '13 05:03

user2102546


2 Answers

Stack is Last In First Out (LIFO), so when you push new view controllers to the stack, previous viewcontroller will not get destroyed( and they remain in memory). When you pop back, there is no need to recreate the Viewcontroller since it is already in memory. So only viewWillAppear gets called.

As to why viewDidAppear doesn't get called in this case, I cant remember where I have read this, but viewDidAppear gets called after your UIViewController's view was added to the application's UIWindow heirarchy. And this process is done before the UIViewController is shown for the first time.

viewDidLoad only called when viewControllers views are loaded into the memory. It will be done when

  1. the first time the view is needed to be shown
  2. sometimes when viewController needed to be reloaded again, because it is purged from memory for some low memory reason.

In your case, when you pop back, the viewController is already loaded, so no need to call viewDidLoad again.

like image 169
Krishnabhadra Avatar answered Sep 19 '22 00:09

Krishnabhadra


Full life cycle of ios ui explain here.enter image description here

http://www.verydemo.com/demo_c134_i4568.html

Note By Abizern from comment: this is true for iOS5 and earlier. iOS6 does not unload views anymore.

like image 34
Hasintha Janka Avatar answered Sep 23 '22 00:09

Hasintha Janka