简体   繁体   English

发布网点的最佳和最安全的做法是什么?

[英]What's the best and safest practice for releasing outlets?

The iOS documentation says that your view controllers should release (and nil, since that's good practice and actually a must for 2.x compatibility) any retained outlets in -dealloc. iOS文档说明你的视图控制器应该释放(并且nil,因为这是良好的做法,实际上是2.x兼容性必须的)-dealloc中任何保留的出口。

The documentation also says that you should do the same for your outlets in -viewDidUnload if you want to allow them being cleaned up when the view is not visible and a memory warning was issued. 文档还说明,如果要在视图不可见时发出清除并且发出内存警告,则应在-viewDidUnload中对其进行相同的操作。

I wonder about two things: 我想知道两件事:

  1. Does this mean that I should duplicate all my outlet releasing code? 这是否意味着我应该复制我的所有插座发布代码? Put it all in -dealloc and in -viewDidUnload? 把它全部放在-dealloc和-viewDidUnload中? That's a real pain to maintain and really easy to forget! 这是一个非常痛苦的维护,很容易忘记!
  2. Am I guaranteed that all my outlets will be re-populated when the view is reloaded after a -viewDidUnload, including my outlets that reference non-view objects in my nib? 我保证在-viewDidUnload之后重新加载视图时会重新填充所有的插件,包括我的笔尖中引用非视图对象的插件吗?

The ideal answer answers both questions above and if there is a distinction between which properties should optimally be released in -dealloc VS -viewDidUnload, clarifies this distinction in detail. 理想的答案回答了上述两个问题,如果在-dealloc VS -viewDidUnload中最佳地释放哪些属性之间存在区别,则详细阐明这一区别。

-viewDidUnload is strictly used for releasing IBOutlets with retain properties. -viewDidUnload 严格用于释放具有保留属性的IBOutlet。

The reason for this has to do with the fact that UIViewController has a view property which it retains. 其原因与UIViewController具有它保留的view属性的事实有关。 That view property itself retains references to all of its subviews. 该视图属性本身保留对其所有子视图的引用。 These subviews are exactly what you are retaining inside these outlet properties. 这些子视图正是您在这些插座属性中保留的内容。 The problem lies in that these subviews have an "extra" retain on them. 问题在于这些子视图对它们有“额外”保留。

The goal of -viewDidUnload is to clear up unnecessary memory usage. -viewDidUnload的目标是清除不必要的内存使用量。 When -viewDidUnload is called, the view property has already been released, which releases the top level UIView along with all its subviews. -viewDidUnload ,视图属性已经被释放,它会释放顶级UIView及其所有子视图。 Since we have retained some of these subviews however, they linger in memory, and we want to release them since they will no longer be used. 由于我们保留了其中一些子视图,因此它们会留在内存中,我们希望释放它们,因为它们将不再使用。 New copies of these subviews will be created when (if) the view is reloaded. 当(如果)重新加载视图时,将创建这些子视图的新副本。 The properties are also set to nil, strictly so we don't have pointers pointing to deallocated memory. 属性也设置为nil,严格来说我们没有指向释放内存的指针。

In -dealloc all retained properties and instance variables should be released. -dealloc ,应释放所有保留的属性和实例变量。 In the case where the -viewDidUnload just executed, you will be sending a harmless [nil release]; 在刚刚执行-viewDidUnload的情况下,您将发送一个无害的[nil release]; to the IBOutlet retained properties you just set to nil. 对于IBOutlet保留的属性,您只需设置为nil。

To add to the excellent answers: 添加到优秀的答案:

With the Apple LLVM 3.0 compiler (and ARC enabled), the need for -dealloc disappears (or at least, the need to release your outlets in it), leaving just -viewDidUnload to deal with. 使用Apple LLVM 3.0编译器(并启用了ARC),-dealloc的需求消失了(或者至少需要释放其中的插座),只留下-viewDidUnload来处理。 As a result, no more duplicate code. 结果,不再有重复的代码。

Nick answered the part 1 of the question perfectly so I skip to part 2 right away. 尼克完全回答了问题的第一部分,所以我马上跳到第二部分。

You are guaranteed, indeed! 确实你得到了保证! When your view gets reloaded from a nib file, everything on that nib file gets reloaded. 当您的视图从nib文件重新加载时,该nib文件上的所有内容都会重新加载。 There is not method to partially load a nib file's contents so if that nib gets reloaded, everything inside will get reloaded, too. 没有部分加载nib文件内容的方法,所以如果重新加载该nib,内部的所有内容也会重新加载。 But everything loaded from a nib has an autorelease on it. 但是从笔尖装入的所有东西都有自动释放。 So you have to retain them. 所以你必须保留它们。 If you have Outlets which retains these freshly loaded classes and you connected those Outlets as outlets on nib, you are good to go. 如果您有奥特莱斯保留这些新装的课程,并且您将这些奥特莱斯作为笔记连接在笔尖上,那么您很高兴。 On a view controller example controller itself is the file's owner of that nib, view is the root element on nib, view being retained by file's owner outlet "view", all other child elements retained by view itself. 在视图控制器上,示例控制器本身是该nib的文件所有者,view是nib上的根元素,视图由文件的所有者出口“view”保留,视图本身保留所有其他子元素。 If you have multiple root elements or elements that root view does not retain then you have to be sure there is outlets for this elements to retain them if nib loading by system automatically, if you are loading nib by a method then you must be aware all those classes are autoreleased when they arrived to your code. 如果您有多个根元素或根视图未保留的元素,那么您必须确保这些元素的出口在系统自动加载nib时保留它们,如果您通过方法加载nib,那么您必须知道所有这些类在到达您的代码时会自动释放。

Simply views are classes too and when a nib file gets loaded, everything inside it gets loaded, not some part of it. 简单地说视图也是类,当nib文件被加载时,其中的所有内容都被加载,而不是它的某些部分。 It is your responsibility to manage outlets. 管理网点是您的责任。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在OpenGL ES中释放内存的最佳方法是什么? - What is best way to releasing memory in OpenGL ES? 扩充UIWebView使用的URL的最佳(最安全)方法是什么? - What is the best (safest) way to augment the URL used by UIWebView? 在iOS块和队列中运行多个任务的最佳实践是什么? - What's the best practice for running multiple tasks in iOS blocks and queues? iOS7 +图像拾取的最佳实践是什么(> 2014) - iOS7+ What's the best practice in Image Picking (>2014) 在iOS应用程序中创建持久性任务队列的最佳做法是什么? - What's the best practice of creating persistent task queue in iOS application? 什么是实现多节TableView的最佳实践 - What's the Best Practice for Implementing Multi-section TableView 在iOS中通过http提交密码的最佳做法是什么? - What's best practice for submitting passwords over http in iOS? 什么是可重用的UITableViewCell和代表的最佳实践? - What is best practice when it comes to reusable UITableViewCell's and delegates? iOS StoreKit旧/升级支持的最佳实践是什么? - What's the best practice for iOS StoreKit legacy/upgrade support? 创建“始终登录” IOS应用程序的最安全方法是什么? - What's the safest way to create an 'always logged in' IOS app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM