1

Whenever we present or push from SourceViewController to DestinationViewController, ViewDidLoad() method called every time in DestinationViewController in xCode 9.4 and iOS 11 or later. And when we pop or dismiss from DestinationViewController to SourceViewController, viewDidLoad() method not called.

vivekDas
  • 1,248
  • 8
  • 12
  • 1
    See viewDidLoad called only once when you create the VC object. Every time you create a new VC object it gets called. – vivekDas Aug 18 '18 at 05:53
  • viewDidLoad is not supposed to be called when popping or dismissing, so the behavior is correct – JohnV Aug 18 '18 at 05:54
  • @vivekDas, debug and check out ViewDidLoad() called without creating any object. If project will be large and method called every time, that time we get memory and loading issues. So i want to sort out that issue. – Divya Patel Aug 18 '18 at 06:02
  • Its not possible that ViewDidLoad will get called without creating and showing the VC. You are checking something wrong. Check the below article. https://roadfiresoftware.com/2015/01/ios-essentials-the-uiviewcontroller-lifecycle/ – vivekDas Aug 18 '18 at 06:13
  • @vivekDas viewDidLoad is not supposed to be called every time when present or push, first time it should, but not every time. in my case its get called every time. in xCode 8 and iOS 10 it wasn't called every time but now in xCode 9.4 and iOS 11 its called every time. – Divya Patel Aug 18 '18 at 06:31
  • post your code , you may be creating the new Instance . – Dhiru Aug 18 '18 at 06:34
  • @DivyaPatel : See the updated answer . – Dhiru Aug 18 '18 at 06:58
  • Add your code how you present your VC. – vivekDas Aug 18 '18 at 07:52

2 Answers2

0

ViewDidload method only called only when the view loads first time. When you present or push it loads the view so, it calls viewdidload. But when you pop it release the view.

shivi_shub
  • 1,018
  • 1
  • 7
  • 15
0

Here is the lifecycle of UIViewController.

  • ViewDidLoad - Called when you create the class and load from xib. (Either present or push) This method called for initial setup and only one time called.
  • ViewWillAppear - Called right before your view appears, this will be called every time your view is about to appear on the screen.
  • ViewDidAppear - Called after the view appears - great place to start an animations or the loading of external data from an API. This will be also called every time after ViewWillAppear when view appeared on the screen.
  • ViewWillDisappear/DidDisappear - Same idea as ViewWillAppear/ViewDidAppear only when view is about to dismiss or pop.
  • ViewDidUnload/ViewDidDispose - In Swift or Objective C, this is where you do your clean-up and release of stuff, but this is handled automatically so not much you really need to do here.

Read Apple documentation for details.

Sunil Targe
  • 7,251
  • 5
  • 49
  • 80