问题
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.
回答1:
Put that code in viewDidAppear or viewWillAppear.
回答2:
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
回答3:
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.
来源:https://stackoverflow.com/questions/46521391/xcode-swift-viewdidload