Satya Satya - 4 months ago 58
iOS Question

viewDidLoad vs ViewWillAppear in IOS

Please help me with this. I have created a simple project with two views as shown. I have attached the images for my storyboard and swift files. So, I read that viewdidload will be executed only once while loading the view into the memory. But, when I make a transition from secondview to the firstview the viewdidload is executing again and so is the print statement in the viewdidload method.

Someone please explain me this.
enter image description hereenter image description here


viewDidLoad is not called once for the Application. It is get called once for that viewController when the view holds memory and loaded.

So as many number of of time you push to the viewController, that many times it will call the viewDidLoad

  • viewDidLoad() — Called when the view controller’s content view (the top of its view hierarchy) is created and loaded

  • viewWillAppear() — Intended for any operations that you want always to occur before the view becomes visible.

For more info about this look at the link :

So if the view is already in memory (Like your case), then no need to push again, only need to pop back by this code