[英]How can I completely reload a view so that viewDidLoad gets run again?
I would like to reload my view so that viewDidLoad
redoes all of its work. 我想重新加载我的视图,以便
viewDidLoad
重做其所有工作。 How can I do that in Swift? 我该如何在Swift中做到这一点?
viewViewDidLoad
and viewWillAppear
are part of the view controller lifecycle and are called automatically by iOS after your UIViewController
got instantiated and before the view appears on the screen. viewViewDidLoad
和viewWillAppear
是视图控制器生命周期的一部分,在实例化UIViewController
之后且视图出现在屏幕上之前,iOS会自动调用它们。 You can call them anywhere in your view controller class, but that's not at all good practice and I would never recommend you to do so! 您可以在视图控制器类中的任何位置调用它们,但这根本不是一个好习惯,我绝不建议您这样做!
If you have code that you want to be executed at multiple times during the lifetime of your view controller, put it into separate methods and call these from viewDidLoad
and from the other parts in your code where you need them. 如果您希望在视图控制器的生命周期内多次执行这些代码,请将其放入单独的方法中,然后从
viewDidLoad
以及需要代码的其他部分中调用它们。
You can read more about the view controller life cycle in Apple's documentation . 您可以在Apple 文档中阅读有关视图控制器生命周期的更多信息 。
You could call ViewDidLoad
manually but it is not recommended. 您可以手动调用
ViewDidLoad
,但不建议这样做。 The best thing is to put all your code inside viewDidLoad
in another methods (like loadData
). 最好的办法是将所有代码放入另一个方法(如
loadData
)的viewDidLoad
中。 And call this method again when you need it. 并在需要时再次调用此方法。
But it depends why you need it. 但这取决于您为什么需要它。 Often
ViewWillAppear
or ViewDidAppear
are a better choice. 通常,
ViewWillAppear
或ViewDidAppear
是更好的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.