简体   繁体   English

如何完全重新加载视图,以便再次运行viewDidLoad?

[英]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. viewViewDidLoadviewWillAppear视图控制器生命周期的一部分,在实例化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. 通常, ViewWillAppearViewDidAppear是更好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何完全重载视图? - How to completely reload view? 如何找到图像路径,以便以后可以重新加载相同图像? - How to find path o image so I can reload later again same image? 强制viewDidLoad在启动时再次运行 - Force viewDidLoad to run again on start 如何确定 PHAsset 是否未完全从 iCloud 下载(因此我需要使用 options.networkAccessAllowed 再次请求) - How to identify if a PHAsset is not completely downloaded from iCloud (so I need to request again with options.networkAccessAllowed) 如何在ViewDidLoad之前运行委托方法 - How can delegate method run before ViewDidLoad 为什么不能将在viewDidLoad方法中创建的视图限制为ViewControllers视图? - Why can I not constrain a view created in the viewDidLoad method to the ViewControllers View? 如何在UITableViewCell中调用viewDidLoad? - How can I call viewDidLoad in a UITableViewCell? 在我的TabBarViewController中,如何为viewDidLoad上的每个子视图控制器设置一个实例变量? - In my TabBarViewController, how can I set an instance variable to every child view controller on viewDidLoad? 如何避免在viewDidLoad中执行此“ if”块? - How can I avoid this “if” block in viewDidLoad to be executed? 如何在 viewDidLoad 上调用 tableView? - How can I call the tableView on viewDidLoad?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM