是否应该全部引用声明为弱的以编程方式创建的子视图? [英] Should all reference to programmactically created subviews declared as weak?

查看:77
本文介绍了是否应该全部引用声明为弱的以编程方式创建的子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在iOS5中使用ARC时,在视图中创建的对子视图的引用都应使用weak或strong关键字声明,这使我感到困惑.

I am getting confuse weither reference to subviews i am creating in a view should be declared with the weak or strong keyword when using ARC in iOS5.

这是我的头文件的示例:

Here is a sample of my header file:

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@class SCLGridView;

@interface MyViewController : UIViewController <UIPopoverControllerDelegate, MFMailComposeViewControllerDelegate>
    @property (weak, nonatomic) IBOutlet UIView *hiddenBrowserView; 
    @property (strong, nonatomic) SCLGridView *gridView;
    @property (strong, nonatomic) UIImageView *backgroundView; 
    @property (strong, nonatomic) UIView *backgroundShadowView;
    @property (strong, nonatomic) UIPopoverController* popOverController;
@end

我的印象是,我正在创建并想要引用的视图应该使用strong关键字声明,因为我拥有这些视图(我创建了它们).但是,我已经声明了hiddenBrowserView为弱,因为我引用的是我在情节提要中创建的视图.这种方法是正确的,还是我应该使所有这些视图引用都变弱,即使对于我以编程方式创建的视图引用也是如此?谢谢!

I run under the impression that the views i am creating and want to reference should be declare with the strong keyword because i am owning those views(i create them). However i have declared the hiddenBrowserView as weak because i am referencing a view i have created in the storyboard. Is this apporach correct or i should make all those view reference as weak even for the reference to views i create programmatically? Thanks!

推荐答案

据我所知,子视图由控制器的主视图强烈引用.因此,将它们强引用是没有目的的,因为当主视图下降时,它们将无用.

As far as I know the subviews are strongly referenced by the main view of your controller. So there is no purpose of referencing them strong because the'll be useless when your main view goes down.

  1. 以其他方式,您可以通过编程创建子视图,然后添加到控制器的主视图中.
  2. 您控制器的主视图强烈引用了它们.
  3. 您会使用属性指向该视图.
  4. 当控制器的主视图是 deallocated 时,子视图也是如此.
  1. In other way, you create a subviews programmatically, and add them to your controller's main view.
  2. Your controller's main view references them strongly.
  3. And you point at this views with weak properties.
  4. When your controller's main view is deallocated, so are the subviews.

这篇关于是否应该全部引用声明为弱的以编程方式创建的子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆