问题
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.
- In other way, you create a subviews programmatically, and add them to your controller's main view.
- Your controller's main view references them strongly.
- And you point at this views with weak properties.
- 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