0

I have two ViewControllers. When I press a button on the FirstViewController the SecondViewController shows up. When I press a button on the SecondViewController the FirstViewController shows up again. The problem is, that the View on the FirstViewController does not load again. The ViewDidLoad loop does not load again. I want to create a loop, which checks a variable every single time the FirstViewController shows up.

  • 4
    Possible duplicate of [Looking to understand the iOS UIViewController lifecycle](https://stackoverflow.com/questions/5562938/looking-to-understand-the-ios-uiviewcontroller-lifecycle) – Matusalem Marques Oct 02 '17 at 07:50
  • maybe the `-viewWillAppear(_:)` is your desired scope; if you are interested more about the life-cycle of the view controllers (__you are!__), please spend time on reading the [Apple docs about it](https://developer.apple.com/documentation/uikit/uiviewcontroller). – holex Oct 02 '17 at 07:59

3 Answers3

1

viewDidLoad is only called when the view first loads. Sounds like you are looking for the viewDidAppear (or viewWillAppear) method which is called every time view has just been (viewDidAppear) or is about to appear (viewWillAppear).

So you're probably looking for either of those methods. You might want to look at the diagram on this page for more information

donnywals
  • 7,241
  • 1
  • 19
  • 27
0

Put that code in viewDidAppear or viewWillAppear.

luk2302
  • 55,258
  • 23
  • 97
  • 137
0

ViewDidLoad method call when you add an instance of UIViewController into Navgation Stack.

I assumed you already know about Navigation Stack.

When you display FirstViewController from SecondViewController it means you did one thing from two possible ways

1. You SecondViewController from Navigation Stack in this way ViewDidLoad of FirstViewController will never call because FirstViewController already loaded in the memory. In this case ViewDidAppear and ViewWillAppear will execute. Because these methods will always call whenever view is going to appear on screen.

2. You created another instance of FirstViewController on SecondViewController and push FirstViewController's instance on Navigation Stack. In this way a new instance of FirstViewController will be added and ViewDidLoad will automatically called. And a looped of FirstViewController and SecondViewController will automatically created.

Hopefully you understand now why ViewDidLoad method never called for you.

Usman Javed
  • 2,437
  • 1
  • 17
  • 26