search

Home  >  Q&A  >  body text

iOS 一个类中申明一个UIButton* butto 属性用strong 和用属性weak区别

假设有一个类 名字叫做Puzzled继承UIViewController
声明了个属性 @property (nonatomic, strong) UIButton *btn;

跟声明了属性 @property (nonatomic, weak) UIButton *btn;
在- (void)viewDidLoad方法中调用 {
UIButton *btn = [[UIButton alloc]init];
self.btn = btn
}的区别是什么

天蓬老师天蓬老师2861 days ago709

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-04-17 16:51:09

    Since the generated button has no reference elsewhere, the property of the weak attribute will be lost after you assign the value.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 16:51:09

    If there are other objects referencing it, use weak, otherwise use strong

    If no object refers to it and it is created out of scope, it will be removed

    Also, when dragging objects from xib or storyboard, then use weak at this time,

    reply
    0
  • Cancelreply