iOS懒加载

博主进行iOS开发有一段时间了(新手哈),经常在看各位大神的博客学习东西,也决定自己开始写写博客,分享分享心得,也希望各位看官大大提出意见哈。

  

设置一个类的属性时(用@property),简单点的可以用他自带的set方法去构造这个对象。

 

- (UILabel *)label

{

    if (_label == nil)

    {

        _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 414, 40)];

        _label.backgroundColor = [UIColor lightGrayColor];

        

    }

    

    return _label;

}

 

在你需要add的view上 例如在Viewdidload里加上

[self.view addSubview:self.label];

 

他的self.会自动去调用set方法,所以在set方法里用的是_label,如果是self.label就会出现badaccess

 

posted @ 2016-03-09 16:18  求知欲。  阅读(126)  评论(0)    收藏  举报