在 viewDidUnload 中释放有什么区别吗?
我是 iPhone 开发新手。
我在我的应用程序中使用一些按钮和标签。我将在 dealloc 函数中释放所有这些按钮和标签。
我也在 viewDidUnload 中释放它们。
- (void)viewDidUnload
{
[super viewDidUnload];
self.ans1 = nil;
self.ans2 = nil;
self.ans3 = nil;
self.ans4 = nil;
self.button = nil;
self.button2 = nil;
self.button3 = nil;
self.button4 = nil;
}
我只是想知道它对内存管理有好处还是对内存管理没有影响。
为什么我们用它?
提前致谢..!!
I am new to iPhone development.
I am using some buttons and labels in my app. I am releasing all these buttons and labels in dealloc function.
I am also releasing them in viewDidUnload.
- (void)viewDidUnload
{
[super viewDidUnload];
self.ans1 = nil;
self.ans2 = nil;
self.ans3 = nil;
self.ans4 = nil;
self.button = nil;
self.button2 = nil;
self.button3 = nil;
self.button4 = nil;
}
I just want to know that it is good for memory management or it makes no difference to memory management.
And why we use that??
Thanks in advance..!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
调用 viewDidUnload 时,卸载的不是视图控制器,而是其视图。 视图控制器在被释放之前一直保持活动状态。
在 viewDidUnload 中,您必须释放属于视图一部分的那些对象以及可以在 viewDidLoad 中重新创建的所有对象(因为 viewDidLoad 将在以下情况下再次调用):视图控制器需要重新创建其视图)。这包括您的所有网点。您还必须将这些变量设置为 nil 以避免过度释放它们。
因为在 dealloc 中,您应该释放视图控制器保留的所有对象,包括那些包含在 viewDidUnload 中的对象。
it is not the view controller that is unloaded when viewDidUnload is called but only its view. The view controller stays alive until it is deallocated.
In viewDidUnload, you have to release those objects that are part of the view and everything that can and will be recreated in viewDidLoad (because viewDidLoad will be called again when the view controller need to recreate its view). This includes all your outlets. You also have to set these variables to nil to avoid overreleasing them.
Because in dealloc, you should release all objects your view controller retains, including those you included in viewDidUnload.
viewDidUnload 用于释放创建视图时可能创建的任何内容 - 这包括 viewDidLoad 中的内容,还包括从 xib 文件内部创建的 IBOutlet 属性。这些都应该被释放并在 viewDidUnload 中设置为 nil。
其他任何东西都应该在 dealloc 中释放。
这个想法是,如果调用 viewDidUnload 来释放一些内存,则可以从 viewDidLoad 方法完全重新创建视图。
另请参阅这个问题;
使用 viewDidUnload 在 iPhone 上进行内存管理
viewDidUnload is used to release anything that you might have made when the view is created - this included things in viewDidLoad but also includes and IBOutlet properties that are created from inside a xib file. these should all be released and set to nil in viewDidUnload.
Anything else should just be released in dealloc.
The idea is that if viewDidUnload is called to free some memory, the view can be recreated again completely from your viewDidLoad method.
Also see this question;
memory management on the iPhone with viewDidUnload
这对于内存管理很有好处。如果在卸载视图控制器时释放与视图控制器关联的对象,则可以有效减少应用程序的内存占用量。即使不使用对象也保留它们会使您的应用程序更容易出现内存警告和内存警告。最终终止。
因此,最好在
ViewDidLoad
& 中进行分配。在ViewDidUnload
中释放。HTH,
阿克谢
It is good for memory management. If you release objects associated with a View controller when the controller unloads, you effectively reduce the memory footprint of your application. Retaining objects even when you are not using them makes your application more prone to memory warnings & eventual termination.
Hence, it is good practice to allocate in
ViewDidLoad
& release inViewDidUnload
.HTH,
Akshay