简体   繁体   English

在ARC上将IBOutlets创建为ivars。 内存问题?

[英]Creating IBOutlets as ivars on ARC. Memory issues?

I'm aware best practices for Objective-C development says IBOutlets should always be defined as properties according to Apple. 我知道,Objective-C开发的最佳实践表明IBOutlets应该始终被Apple定义为属性。

From a practical perspective, in iOS and OS X outlets should be defined as declared properties. 从实际的角度来看,在iOS和OS X中,出口应定义为已声明的属性。 Outlets should generally be weak, except for those from File's Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong. 出口通常应该较弱,但从文件所有者到笔尖文件(或iOS中的情节提要场景)中顶级对象的出口则应较坚固。

But for learning purposes let's say we got the following scenario using ARC: 但是出于学习目的,我们使用ARC获得了以下方案:

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController{
     IBOutlet UIButton *buttonA;
     IBOutlet UIButton *buttonB;
     IBOutlet UIButton *buttonC;
}

@end

If I'm not mistaken, those three buttons are strong, so my question is: Will those buttons be released from memory once the ViewController is released? 如果我没记错的话,那三个按钮很坚固,所以我的问题是:一旦释放ViewController,这些按钮是否会从内存中释放出来?

Those buttons would be released automatically if they were weak, I know that, but not sure if they are strong. 我知道,如果这些按钮较弱,它们会自动释放,但不确定它们是否较强。

Can anyone please help? 谁能帮忙吗? Just to be clear, the method 'dealloc' on DetailViewController is empty. 请注意,DetailViewController上的方法'dealloc'为空。

You asked: 您询问:

If I'm not mistaken, those three buttons are strong , so my question is: Will those buttons be released from memory once the ViewController is released? 如果我没记错的话,那三个按钮很strong ,所以我的问题是:释放ViewController后,这些按钮会从内存中释放吗?

Yes. 是。 Or, more accurately, those buttons will be deallocated when there are no more strong references. 或者,更准确地说,当没有更强的引用时,这些按钮将被释放。 And in this scenario, those buttons now have two strong references, one being the view controller and another being the view to which these buttons were added as subviews. 在这种情况下,这些按钮现在具有两个strong引用,一个是视图控制器,另一个是将这些按钮添加为子视图的视图。 Both of those strong references would need to be relieved before the button would be deallocated. 在释放按钮之前,都需要释放这两个strong引用。

But why would you want to maintain two strong references to that control? 但是,为什么要保持对该控件的两个强烈引用呢? Generally you let the view maintain the strong reference to its subviews (ie let the view "own" its subviews), and the view controller is only using a weak reference to those subviews. 通常,让视图维护对其子视图的strong引用(即,让视图“拥有”其子视图),并且视图控制器仅对这些子视图使用weak引用。

Those buttons would be released automatically if they were weak , I know that, but not sure if they are strong . 我知道,如果这些按钮weak ,它们会自动释放,但是不确定它们是否strong

When the view controller has the weak reference, the buttons are being released because the only strong reference to the button is maintained by its superview and when that view is removed, then the button will lose its last strong reference and can be deallocated. 当视图控制器具有weak引用时,按钮将被释放,因为对该按钮的唯一strong引用是由其父视图维护的,并且当删除该视图时,该按钮将失去其最后的强引用并可以被释放。

If the view controller has a strong reference, you are unnecessarily added another strong reference that needs to be relieved before the buttons are deallocated. 如果视图控制器具有strong引用,则不必要地添加了另一个strong引用,在释放按钮之前需要释放该strong引用。 You can do that, but it's unnecessary. 您可以这样做,但这不是必需的。 You quoted from the Resource Programming Guide , but the preceding sentence says "you don't need strong references to objects lower down in the graph because they're owned by their parents, and you should minimize the risk of creating strong reference cycles." 您引用了《 资源编程指南》中的内容 ,但前面的句子说:“您不需要对图下方的对象的强引用,因为它们是其父级拥有的,因此应将创建强引用周期的风险降到最低。”

Bottom line, your example with the implicitly strong references to the IBOutlet controls will work fine. 最重要的是,您的示例具有对IBOutlet控件的隐式strong引用。 But there's no advantage to having the view controller maintain a strong reference to the buttons and it represents a bit of a misunderstanding of the object graph. 但是,让视图控制器保持对按钮的strong引用没有任何好处,这表示对对象图有些误解。 View controllers really should only be maintaining weak references to the controls on their views. 实际上,视图控制器只应在其视图上维护对控件的weak引用。

Yes, when the ViewController is released, even its strongly referenced objects will be released. 是的,当释放ViewController时,甚至会释放其强烈引用的对象。 (Unless of course, some other non-released object holds a strong reference to them.) (当然,除非其他一些未发布的对象对此有很强的引用。)

There's a good discussion of weak and strong here: Explanation of strong and weak storage in iOS5 这里对弱和强进行了很好的讨论: iOS5中强和弱存储的说明

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

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