【iOS开发】添加子控件方式(懒加载,GCC)

时间:2023-03-09 06:46:20
【iOS开发】添加子控件方式(懒加载,GCC)
 //
 //  ViewController.m
 //  GCC
 //
 //  Created by admin on 15/10/7.
 //  Copyright © 2015年 admin. All rights reserved.
 //

 #import "ViewController.h"

 @interface ViewController ()

 @property (nonatomic, strong) UIButton* btn;

 @property (nonatomic, strong) UIButton* blueButton;

 @end

 @implementation ViewController

 - (void)viewDidLoad
 {
     [super viewDidLoad];

     UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
     btn.backgroundColor = [UIColor redColor];

     [self.view addSubview:btn];
     [self.view addSubview:self.btn];
     [self.view addSubview:self.blueButton];
 }

 ///  GCC
 - (UIButton*)blueButton
 {
     if (!_blueButton) {

         _blueButton = ({
             UIButton* btn = [[UIButton alloc] initWithFrame:({
                                                   CGRect frame = CGRectMake(, , , );
                                                   frame;
                                               })];
             btn.backgroundColor = [UIColor blueColor];
             btn;
         });
     }
     return _blueButton;
 }

 ///  懒加载的方式
 - (UIButton*)btn
 {
     if (_btn == nil) {
         _btn = [[UIButton alloc] initWithFrame:CGRectMake(, , , )];
         _btn.backgroundColor = [UIColor greenColor];
     }

     return _btn;
 }

 @end