简体   繁体   English

UIButton从记忆中释放出来?

[英]UIButton released from memory?

I create a couple of UIButtons (with selectors) in a method (throughout the life cycle of the app) and stick them all into an NSMutableArray. 我在一个方法中创建了几个UIButtons(带选择器)(在应用程序的整个生命周期中)并将它们全部粘贴到NSMutableArray中。 The created buttons are NOT properties but the array is. 创建的按钮不是属性,但是数组是。

  1. If I delete a button from the array, does the button automatically get released from memory? 如果我从数组中删除一个按钮,该按钮会自动从内存中释放吗?
  2. If the answer to the above is no, what do I have to do to get it released from memory? 如果上面的答案是否定的,我该如何才能从内存中释放它?

The button will indeed, eventually, be released from memory as no one is retaining it. 该按钮最终将从内存中释放,因为没有人保留它。 Its retain count will be decremented regardless, but only if no one else is retaining it, it will be released from memory. 无论如何,它的保留计数都会减少,但只有当没有其他人保留它时,它才会从内存中释放出来。

This is however a somewhat confusing mechanism - keep in mind that the UIButtons are views and are probably a part of your view hierarchy which also retains them. 然而,这是一个有点令人困惑的机制 - 请记住,UIButtons是视图,可能是视图层次结构的一部分,它也保留了它们。 If they're also visible, they must be removed from their superview in order to be released. 如果它们也可见,则必须将它们从超级视图中删除才能被释放。

Anything you put in a NSArray (and a NSMutableArray ) gets retained by the array, and released when removed from the array or when the array itself is released. 您放入NSArray (和NSMutableArray )的任何内容都会被数组保留,并在从数组中删除或释放数组本身时释放。 It's true for buttons and any other object. 按钮和任何其他对象都是如此。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM