iOS懒加载

2023-09-25  本文已影响0人  ttdiOS

iOS懒加载

-(UILabel *)userNameLab {

    if(!_userNameLab) {

        _userNameLab = [[UILabel alloc] init];

        _userNameLab.textColor = [UIColor whiteColor];

        _userNameLab.font = [CommonUtil getFontWithFontName:@"PingFangSC-Regular" andFontSize:18];

        [_userNameLab setAdjustsFontSizeToFitWidth:YES];

        [_userNameLab setMinimumScaleFactor:0.1];

        [_userNameLab setTextAlignment:NSTextAlignmentCenter];

        [self.view addSubview:_userNameLab];

    }

    return_userNameLab;

}

这样写 很不好

懒加载里 有一行:

 [self.view addSubview:_userNameLab];

假若提前 用了这个 userNameLab

再用其他的这样的懒加载 userNameLab2

就会存在 userNameLab2 把 userNameLab给 覆盖了,同样 布局下

一般不在 懒加载里 写   [self.view addSubview:_userNameLab];

上一篇 下一篇

猜你喜欢

热点阅读