简体   繁体   中英

Xcode Swift ViewDidLoad

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.

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

将该代码放入viewDidAppearviewWillAppear

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM