[英]Xcode Swift ViewDidLoad
I have two ViewControllers. 我有两个ViewController。 When I press a button on the FirstViewController the SecondViewController shows up.
当我按下FirstViewController上的按钮时,SecondViewController出现。 When I press a button on the SecondViewController the FirstViewController shows up again.
当我按下SecondViewController上的按钮时,FirstViewController再次出现。 The problem is, that the View on the FirstViewController does not load again.
问题是,FirstViewController上的View不会再次加载。 The ViewDidLoad loop does not load again.
ViewDidLoad循环不会再次加载。 I want to create a loop, which checks a variable every single time the FirstViewController shows up.
我想创建一个循环,每次FirstViewController出现时都会检查一个变量。
viewDidLoad
is only called when the view first loads. 仅在首次加载视图时调用
viewDidLoad
。 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
). 听起来好像您在寻找
viewDidAppear
(或viewWillAppear
)方法,该方法在每次视图刚被( viewDidAppear
)或即将出现( 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
您可能需要查看此页面上的图以获取更多信息
将该代码放入viewDidAppear
或viewWillAppear
。
ViewDidLoad method call when you add an instance of UIViewController
into Navgation Stack
. 将
UIViewController
的实例添加到Navgation Stack
时,将调用ViewDidLoad方法。
I assumed you already know about Navigation Stack
. 我以为您已经了解
Navigation Stack
。
When you display FirstViewController
from SecondViewController
it means you did one thing from two possible ways 当您从
SecondViewController
显示FirstViewController
,这意味着您通过两种可能的方式做了一件事情
1. You SecondViewController
from Navigation Stack
in this way ViewDidLoad
of FirstViewController
will never call because FirstViewController
already loaded in the memory. 1.您
SecondViewController
从Navigation Stack
这样ViewDidLoad
的FirstViewController
永远不会调用因为FirstViewController
在内存中已经加载。 In this case ViewDidAppear
and ViewWillAppear
will execute. 在这种情况下,将执行
ViewDidAppear
和ViewWillAppear
。 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
. 2.你创造的另一个实例
FirstViewController
上SecondViewController
推FirstViewController
上的实例Navigation Stack
。 In this way a new instance of FirstViewController
will be added and ViewDidLoad
will automatically called. 这样,将添加新的
FirstViewController
实例,并自动调用ViewDidLoad
。 And a looped of FirstViewController
and SecondViewController
will automatically created. 然后将自动创建一个循环的
FirstViewController
和SecondViewController
。
Hopefully you understand now why ViewDidLoad
method never called for you. 希望您现在了解为什么从未调用过
ViewDidLoad
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.