1.
从storyboard
或者xib
上创建控件,在控件放在view
上的时候,已经形成了如下的引用关系,
以UIButton
为例:UIViewController->UIView->subView->UIButton
然后你为这个UIButton
声明一个weak
属性@property(nonatomic,weak) IBOOutlet UIButton *btn;
相当于xib/sb对这个Button是强引用,你声明的属性对它是弱引用。2.手动创建控件a). 将控件声明成strong@property(nonatomic,strong) UIButton *btn;
那么你在实现这个控件时只需这样:
_btn = [[UIButton alloc]init];
[self.view addSubview:_btn]
b). 将控件声明成weak@property(nonatomic,weak) UIButton *btn;
那么你在实现这个控件时需要这样:
UIButton *button = [[UIButton alloc]init];
_btn = button;
[self.view addSubview:_btn];