视图控制器中的资源管理 Resource Management in View Controllers [苹果官方文档的翻译]

View controllers are an essential part of managing your app’s resources. View controllers allow you to break your app up into multiple parts and instantiate only the parts that are needed. But more than that, a view controller itself manages different resources and instantiates them at different times. For example, a view controller’s view hierarchy is instantiated only when the view is accessed; typically, this occurs only when the view is displayed on screen. If multiple view controllers are pushed onto a navigation stack at the same time, only the topmost view controller’s contents are visible, which means only its views are accessed. Similarly, if a view controller is not presented by a navigation controller, it does not need to instantiate its navigation item. By deferring most resource allocation until it is needed, view controllers use less resources.

视图控制器是管理应用资源的重要部分。视图控制器允许你把应用分散到不同的部分,只是在需要的时候实例他们。除了这个,视图控制器本身管理不同的资源,并在不同的时候实例化他们。例如,视图控制器的视图层次只有在视图被获取的时候(通常就是视图被显示到屏幕上时)才被实例化。如果同时,多个视图控制器被推入导航栈时,只有最上面的视图控制的视图是可见的,这意味着它的视图是可以被访问的。相似地,如果导航控制器没有显示视图控制器,视图控制器就没必要实例化导航项。知道需要时才分配资源,这样视图控制器可以节约内存。

When memory available to the app runs low, all view controllers are automatically notified by the system. This allows the view controller to purge caches and other objects that can be easily recreated later when memory is more plentiful. The exact behavior varies depending on which version of iOS your app is running on, and this has implications for your view controller design.

当应用可用的内存较低时,系统自动通知所有的视图控制器。这允许视图控制器清除缓存,以及其他对象,这些对象可以在内存充裕的时候重新创建。这些特性和iOS系统有关。

Carefully managing the resources associated with your view controllers is critical to making your app run efficiently. You should also prefer lazy allocation; objects that are expensive to create or maintain should be allocated later and only when needed. For this reason, your view controllers should separate objects needed throughout the lifetime of the view controller from objects that are only necessary some of the time. When your view controller receives a low-memory warning, it should be prepared to reduce its memory usage if it is not visible onscreen.

有效运行程序需要小心的管理和视图控制器相关的资源。你应该懒加载。对于创建或者保留代价昂贵的对象来说,只有在需要的才加载。因此,视图控制器要区分整个控制器生命周期内都需要的对象和只是在某个时间才需要的对象。当视图控制器收到内存警告时,如果它在屏幕不可见,应该准备减少内存的使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值