Xcode Swift ViewDidLoad

六月ゝ 毕业季﹏ 提交于 2019-12-11 15:05:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!