`
bengan
  • 浏览: 201698 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

viewDidUnload viewDidLoad UIViewController内存管理相关的几个方法

阅读更多
viewDidUnload viewDidLoad UIViewController内存管理相关的几个方法

viewDidLoad
view载入后的操作有:view显示的一些数据要在此载入,并在viewDidUnload时释放。
viewDidUnload
view释放后的操作有:释放那些view显示时的数据,并在view再次载入内存时也载入这些数据。
所以:获得数据应该是先判定是否存在。不存在再去获取。。这么个思路、
dealloc
Deallocates the memory occupied by the receiver.
- (void)dealloc
Discussion
Subsequent messages to the receiver may generate an error indicating that a message was sent to a deallocated object (provided the deallocated memory hasn’t been reused yet).
You never send a dealloc message directly. Instead, an object’s dealloc method is invoked indirectly through therelease NSObject protocol method (if the release message results in the receiver's retain count becoming 0). SeeMemory Management Programming Guide for more details on the use of these methods.
Subclasses must implement their own versions of dealloc to allow the release of any additional memory consumed by the object—such as dynamically allocated storage for data or object instance variables owned by the deallocated object. After performing the class-specific deallocation, the subclass method should incorporate superclass versions of dealloc through a message to super:
- (void)dealloc {
    [companion release];
    NSZoneFree(private, [self zone])
    [super dealloc];
}
Important: Note that when an application terminates, objects may not be sent a dealloc message since the process’s memory is automatically cleared on exit—it is more efficient simply to allow the operating system to clean up resources than to invoke all the memory management methods. For this and other reasons, you should not manage scarce resources in dealloc—see Object Ownership and Disposal in Memory Management Programming Guide for more details.



didReceiveMemoryWarning
Sent to the view controller when the application receives a memory warning.
- (void)didReceiveMemoryWarning
Discussion
The default implementation of this method checks to see if the view controller can safely release its view. This is possible if the view itself does not have a superview and can be reloaded either from a nib file or using a customloadView method. If the view can be released, this method releases it and calls the viewDidUnload method.
You can override this method (as needed) to release any additional memory used by your view controller. If you do, be sure to call the super implementation at some point to allow the view controller to release its view. In iOS 3.0 and later, if your view controller holds references to objects in the view hierarchy, you should release those references in the viewDidUnload method instead. In earlier versions of iOS, you should continue to release them from this method. See the discussion in the viewDidUnload method for information about how to safely release outlets and other objects.



viewDidUnload
Called when the controller’s view is released from memory.
- (void)viewDidUnload
Discussion
This method is called as a counterpart to the viewDidLoad method. It is called during low-memory conditions when the view controller needs to release its view and any objects associated with that view to free up memory. Because view controllers often store references to views and other view-related objects, you should use this method to relinquish ownership in those objects so that the memory for them can be reclaimed. You should do this only for objects that you can easily recreate later, either in your viewDidLoad method or from other parts of your application. You should not use this method to release user data or any other information that cannot be easily recreated.
Typically, a view controller stores references to objects using an outlet, which is a variable or property that includes the IBOutlet keyword and is configured using Interface Builder. A view controller may also store pointers to objects that it creates programmatically, such as in the viewDidLoad method. The preferred way to relinquish ownership of any object (including those in outlets) is to use the corresponding accessor method to set the value of the object tonil. However, if you do not have an accessor method for a given object, you may have to release the object explicitly. For more information about memory management practices, see Memory Management Programming Guide.
By the time this method is called, the view property is nil.
Special Considerations
If your view controller stores references to views and other custom objects, it is also responsible for relinquishing ownership of those objects safely in its dealloc method. If you implement this method but are building your application for iOS 2.x, your dealloc method should release each object but should also set the reference to that object to nil before calling super.
分享到:
评论

相关推荐

    IOS 中loadView,viewDidLoad,viewDidUnload详解及使用

    在iOS开发中,了解UIViewController的生命周期方法至关重要,特别是`loadView`, `viewDidLoad`, 和 `viewDidUnload`。这三个方法在视图控制器的视图管理过程中扮演着不同的角色。 首先,我们来详细解读`loadView`。...

    解析iOS内存不足时的警告以及处理过程

    2. **View Controller**:每个`UIViewController`子类应当实现`didReceiveMemoryWarning`方法。当内存警告发生时,系统会调用此方法。默认情况下,如果控制器的`view`不在当前显示的视图层次结构中,且已实现了`...

    简单阐述一下Objective c

    3. `viewDidUnload`:在系统内存紧张时,如果UIViewController的视图不在当前的视图层次结构中,且控制器实现了`loadView`方法,系统会调用此方法。这里应该释放与视图相关的所有资源,包括IBOutlets并将其设置为nil...

    Cocoa常用类和方法

    本文将详细讲解标题“Cocoa常用类和方法”中涉及的几个核心类:UIViewController、UIView以及UILabel。 首先,我们来看UIViewController。它是iOS应用中控制器层的核心类,用于管理屏幕上的用户界面。...

    cocoa常用类及方法

    `UIViewController` 是一个控制器类,它的主要职责是管理一个或者多个`UIView`实例。`UIViewController`的`view`属性是它默认显示的视图,你可以通过这个属性设置自定义的视图类。`initWithNibName:bundle:`是初始化...

    iOS 工程师面试题

    - UIViewController显示过程中的方法调用顺序是init -> viewDidLoad -> viewDidAppear -> viewDidUnload。 代码题部分: 1. 计算二维数组对角线的值的和,可以通过双重循环实现,从第一行第一列开始,到最后一行...

    Viewrrlkalkadsva

    nib 文件创建的,或者在`loadView`方法中调用了`super.loadView`,当收到内存警告并且View不再使用且已经消失时,ViewController会释放View并将指针置为nil,同时调用`viewDidUnload`方法。 #### 三、代码组织与...

    ios面试题汇总(含答案)

    - **解释**: `UIViewController`类中的方法按特定顺序被调用:首先调用`init`方法完成初始化,然后加载视图(`viewDidLoad`),当视图出现在屏幕上时调用`viewDidAppear`,最后当视图卸载时调用`viewDidUnload`。

    福昕软件招聘iOS开发工程师面试题

    1. UIViewController调用顺序:`initWithNibName:bundle:` -> `loadView` -> `viewDidLoad` -> 使用视图 -> `viewDidUnload` -> `dealloc`。`initWithNibName:bundle:`初始化,`loadView`加载视图,`viewDidLoad`...

    IOS部分面试题

    在iOS开发领域,面试题通常会涵盖多个方面,包括但不限于设计模式、UI组件、网络通信、内存管理以及多线程等。以下是对标题和描述中提及的一些知识点的详细解释: **1. MVC(Model-View-Controller)架构模式:** ...

    iOS面试题-iOS部分

    在iOS面试中,掌握核心知识点至关重要,这有助于应聘者展示其专业技能和理解力。...同时,还需要了解其他关键概念,如AutoLayout、手势识别、内存管理、动画、多线程优化、网络编程、Core Data等。

    ios面试资料

    - **动态库**:与静态库不同,动态库在程序运行时由操作系统动态加载到内存中,多个程序可以共享同一份库的内存副本,从而节省空间。然而,动态库的使用需要确保运行环境已经安装了相应的库,否则程序可能无法运行...

Global site tag (gtag.js) - Google Analytics