ios viewWillAppear、viewDidAppear、viewWillDisappear、viewDidDisappear 的准则

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7386333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:28:33  来源:igfitidea点击:

Guidelines for viewWillAppear, viewDidAppear, viewWillDisappear, viewDidDisappear

objective-ciosuiviewcontrollerviewwillappearviewdidappear

提问by Lorenzo B

Are there any guidelines for using these methods in the right manner? In particular, I would like to know what type of code I could use inside them.

是否有任何以正确方式使用这些方法的指南?特别是,我想知道我可以在其中使用什么类型的代码。

For example, if I have to call a method that retrieves data from a WS, where do I have to call it? Where can I register/unregister a NSNotification? etc.

例如,如果我必须调用一个从 WS 检索数据的方法,我必须在哪里调用它?我在哪里可以注册/取消注册 NSNotification?等等。

回答by Manlio

From UIViewController

UIViewController

viewWillAppear:

视图将出现:

This method is called before the receiver's view is about to be displayed onscreen and before any animations are configured for showing the view. You can override this method to perform custom tasks associated with presenting the view. For example, you might use this method to change the orientation or style of the status bar to coordinate with the orientation or style of the view being presented. If you override this method, you must call super at some point in your implementation.

在接收者的视图即将显示在屏幕上之前以及在配置任何动画以显示视图之前调用此方法。您可以覆盖此方法以执行与呈现视图相关的自定义任务。例如,您可以使用此方法更改状态栏的方向或样式,以与所呈现视图的方向或样式协调。如果覆盖此方法,则必须在实现中的某个时刻调用 super。

viewWillDisappear:

视图将消失:

This method is called in response to a view being removed from its window or covered by another view. This method is called before the view is actually removed or covered and before any animations are configured.

Subclasses can override this method and use it to commit editing changes, resign the first responder status of the view, or perform other relevant tasks. For example, you might use this method to revert changes to the orientation or style of the status bar that were made in the viewDidDisappear: method when the view was first presented. If you override this method, you must call super at some point in your implementation.

调用此方法是为了响应从其窗口中移除或被另一个视图覆盖的视图。在实际移除或覆盖视图之前以及配置任何动画之前调用此方法。

子类可以覆盖此方法并使用它来提交编辑更改、放弃视图的第一响应者状态或执行其他相关任务。例如,您可以使用此方法来恢复在 viewDidDisappear: 方法中首次呈现视图时对状态栏的方向或样式所做的更改。如果覆盖此方法,则必须在实现中的某个时刻调用 super。

viewDidAppear:

viewDidAppear:

You can override this method to perform additional tasks associated with presenting the view. If you override this method, you must call super at some point in your implementation.

您可以覆盖此方法以执行与呈现视图相关的其他任务。如果覆盖此方法,则必须在实现中的某个时刻调用 super。

viewDidDisappear:

viewDidDisappear:

You can override this method to perform additional tasks associated with dismissing or hiding the view. If you override this method, you must call super at some point in your implementation.

您可以覆盖此方法以执行与关闭或隐藏视图相关的其他任务。如果覆盖此方法,则必须在实现中的某个时刻调用 super。

For further information you may check View Controller Programming Guide for iOS

有关更多信息,您可以查看 iOS 视图控制器编程指南

回答by Yunus Nedim Mehel

An addition to the answer: You should invoke methods like super viewWillAppear: at the beginning of your implementation, and invoke viewDidAppear: at the end of your method. Superclass should begin with the initialisation and should be last to terminate.

答案的补充:您应该在实现的开始调用 super viewWillAppear: 之类的方法,并在方法结束时调用 viewDidAppear: 。超类应该从初始化开始,最后终止。