4

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

Swifty
  • 65
  • 1
  • 8

3 Answers3

10

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 : https://developer.apple.com/library/content/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson4.html

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

self.navigationController?.popViewControllerAnimated(true)
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
  • Thanks for the response. would you please explain it with an example, and also does a ViewController's object will be deleted from the memory if we make a transition to another one?? – Swifty Oct 24 '16 at 06:20
  • Yes, the memory of viewController object will be deleted when you pop back from the viewController. For the example, just put break point in your project in viewDidLoad, viewWillAppear, Then see when it is get called. You can check in this way, take a integer variable as 10 in first VC before pushing to second VC, then push to second VC and use popViewController method to come back to first VC, and print for the value of variable you will see 10, but if you come to same vc by pushing you wont get 10 (It is 0 or some random value). To be continued .. – Janmenjaya Oct 24 '16 at 06:27
  • Which concludes that, the view of the screen in which was loaded previously is not the same one it is different. Or best to check is put the back ground color of main view and check with the same way i have explained, then can know the view of the screen is same or different. Hope it helps, if so please accept the answer and up vote. – Janmenjaya Oct 24 '16 at 06:31
  • So, how does it work in case of the Navigation controller. Does it stores the values of unlimited view controller objects if we keep on adding new view controller to the navigation controller's stack – Swifty Oct 24 '16 at 06:39
  • Yes, it keeps in navigation stack. You can check it from navigation stack. using the method "navigationController.viewControllers". which will return you array of controllers stored in the navigation stack. up vote the answer as well. – Janmenjaya Oct 24 '16 at 06:40
  • Thanks for the help. – Swifty Oct 24 '16 at 06:41
  • Glad to hear that. – Janmenjaya Oct 24 '16 at 06:45
1

You should not make transition from secondViewController to firstViewController for back. Pop the second view controller by this code to back:

self.navigationController?.popViewControllerAnimated(true)

When you make a transition it makes a new instance from your firstViewController but when you pop the second view controller it dismiss your secondViewController and shows your last viewed viewController again.

Or

in the case that you are not using navigationController you should use below code to dismiss your secondViewController

self.dismissViewControllerAnimated(true, completion: {});

The main point is that you should not use new transition for back.

Husein Behboudi Rad
  • 5,434
  • 11
  • 57
  • 115
0

The simplest way:

1.First embed your ViewController in NavigationController

2.Call to this (instead of create segue for backing)

navigationController?.popToRootViewController(animated: true)

viewDidLoad will be called only once

letanthang
  • 390
  • 5
  • 7