Should all reference to programmactically created subviews declared as weak?

空扰寡人 提交于 2019-12-14 02:12:26

问题


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

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!


回答1:


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. 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.


来源:https://stackoverflow.com/questions/9343092/should-all-reference-to-programmactically-created-subviews-declared-as-weak

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!