2

I am making iPhone app on swift,here ViewDidAppear and ViewWillAppear is being called multiple times on one viewDidLoad in ios swift app?

please help me.

sulabh qg
  • 1,155
  • 2
  • 12
  • 20

2 Answers2

9

Whenever You push a viewController, It will call viewDidLoad then viewWillAppear and last viewDidAppear. But if you are coming back to viewController only viewWillAppear and viewDidAppear will call.

View will call these two (viewWillAppear & viewDidAppear) every time, whenever this viewController shows on every condition

i. On push - viewDidLoad after it, viewWillAppear & viewDidAppear.

ii. On back or pop - No viewDidLoad will call, Only viewWillAppear & viewDidAppear.

iii. On become active state - No viewDidLoad will call, Only viewWillAppear & viewDidAppear.

Mohit Tomar
  • 5,173
  • 2
  • 33
  • 40
4

If you are not familiar with the UIViewController life cycle I really recommend you to start here Apple documentation and here.

But in a very short answer

The method viewDidLoad - This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method. You usually override this method to perform additional initialization on views that were loaded from nib files.

The methods ViewDidAppear and ViewWillAppear as they sound to you, called every time the view Appear on the screen.

This image (from Apple documentation) shows the valid state transitions between various view ‘will’ and ‘did’ callback methods

enter image description here

Community
  • 1
  • 1
Guy Kahlon
  • 4,510
  • 4
  • 30
  • 41