viewWillAppear、viewDidAppear、viewWillDisappear、viewDidDisappear 的准则 [英] Guidelines for viewWillAppear, viewDidAppear, viewWillDisappear, viewDidDisappear

查看:20
本文介绍了viewWillAppear、viewDidAppear、viewWillDisappear、viewDidDisappear 的准则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有正确使用这些方法的指导方针?我特别想知道我可以在其中使用什么类型的代码.

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

解决方案

来自 UIViewController

viewWillAppear:

<块引用>

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

viewWillDisappear:

<块引用>

当视图从其窗口中移除或被另一个视图覆盖时,会调用此方法.在实际移除或覆盖视图之前以及配置任何动画之前调用此方法.

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

viewDidAppear:

<块引用>

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

viewDidDisappear:

<块引用>

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

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

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.

解决方案

From 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.

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.

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.

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.

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

这篇关于viewWillAppear、viewDidAppear、viewWillDisappear、viewDidDisappear 的准则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆