首页 > 为什么iOS的Masonry中的self不会循环引用?

为什么iOS的Masonry中的self不会循环引用?

UIButton *testButton = [[UIButton alloc] init];
[self.view addSubview:testButton];
testButton.backgroundColor = [UIColor redColor];
[testButton mas_makeConstraints:^(MASConstraintMaker *make) {
    make.width.equalTo(@100);
    make.height.equalTo(@100);
    make.left.equalTo(self.view.mas_left);
    make.top.equalTo(self.view.mas_top);
}];
[testButton bk_addEventHandler:^(id sender) {
    [self dismissViewControllerAnimated:YES completion:nil];
} forControlEvents:UIControlEventTouchUpInside];

如果我用blocksKit的bk_addEventHandler方法, 其中使用strong self, 该viewController就无法dealloc, 我理解是因为,self retain self.view, retain testButton, retain self.
但是如果只用Mansonry的mas_makeConstraints方法, 同样使用strong self, 该viewController却能正常dealloc, 请问这是为什么, 为什么Masonry没有导致循环引用


是否会引起循环引用你只要看函数内部是否copy了这个block就知道了.
像Mansonry

没有copy 使用完就释放了 是不会造成循环引用的.


当有可能出现循环引用的时候不是会warnning么。
这么改

    __weak id weakSelf = self;
    [testButton bk_addEventHandler:^(id sender) {
        __strong <#你的VC类#> strongSelf = weakSelf;
        [strongSelf dismissViewControllerAnimated:YES completion:nil];
    } forControlEvents:UIControlEventTouchUpInside];

可以避免循环引用。不要问我为什么,我也不太明白,只是大家都这么写。

【热门文章】
【热门文章】