user2102546
user2102546

Reputation: 87

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

Upvotes: 8

Views: 7699

Answers (4)

Hasintha Janka
Hasintha Janka

Reputation: 1639

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.

Upvotes: 13

Niru Mukund Shah
Niru Mukund Shah

Reputation: 4733

First of all, nice question @user2102546. Checkout here the perfect reason for your query.

viewDidLoad only gets called if view controller's views were unloaded, and need to be reloaded.

Normally, if you use a navigation controller, and go back to a previous view with one of the pop methods, viewDidLoad does not get called again, because the view are not unloaded yet.

However, the system can unload the views of any view controller when it is not frontmost in order to free up memory, so viewDidLoad can get called any time a view controller is about to be presented. You need to write your code to respond correctly to the different events.

Enjoy Programming!!

Upvotes: 3

Krishnabhadra
Krishnabhadra

Reputation: 34296

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.

Upvotes: 18

Nathan
Nathan

Reputation: 190

I don't have a complete answer for you but I hope this helps.

viewDidLoad is a callback for modifying a view after the load event has happened. In your case, the view has already loaded. The fact that it is not in view doesn't mean it has been unloaded from memory.

viewDidAppear: While I don't know why this event isn't firing and would be happy if someone else would fill in the gap.

Upvotes: 2

Related Questions