Question

I understand that you can use ViewDidLoad to run some code when a view is loaded. However that only happens once. How can I run a method every single time that view is shown. So for example: let says you are currently in ViewController A and you press a UIButton to go to ViewController B. Then you press a button to go back to ViewController A, how would you then re-run the ViewDidLoad code??

I hope my question makes sense. In essence I want to re-run a small method every single time the user is on a particular ViewController.

Thanks for your time, Dan.

Was it helpful?

Solution

viewWillAppear:

Notifies the view controller that its view is about to be added to a view hierarchy.

or

viewDidAppear:

Notifies the view controller that its view was added to a view hierarchy.

OTHER TIPS

This answer didn’t help. I found myself in the same situation and the solution is simple.

  1. Create a method with codes you want to be executed every time your view controllerA shows up.
  2. Place your method under viewDidAppear()
  3. Make sure that the modal type of your viewcontrollerB is set to fullscreen (fullscreen removes viewcontrollerA from the stack)
  4. Under viewcontrollerB viewDidLoad() Set its background other color than clear. White works for me
  5. Dismiss from your viewControllerB and “voila!”

Create an AbstractController that every view controller inherits from that controller, and you override the viewDidLoad method and make it do whatever you want at every view opened.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top