视图生命周期

2016-03-08  本文已影响30人  Angelia_
//1. 初始化方法
//第一个参数:nibNameOrNil 可视化文件的名称
//第二个参数:nibBundleOrNil 当前的包名
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        NSLog(@"%s--%d",__FUNCTION__,__LINE__);
    }

    return self;
}
//2.加载视图
#warning --如果要重写loadView方法,必须指定根视图
-(void)loadView{
    
    //重写loadView需要指定一个根视图
    LoginView * view = [[LoginView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    view.backgroundColor = [UIColor yellowColor];
    self.view = view;
    
    NSLog(@"%s--%d",__FUNCTION__,__LINE__);
}

//3.视图已经被加载 视图控制器
- (void)viewDidLoad{
    
    [super viewDidLoad];

    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
    backView.backgroundColor = [UIColor blackColor];
    backView.tag = 666;
    [self.view addSubview:backView];
       
    
    // 默认颜色为透明色 一般设为白色
    // 设置背景色
//    self.view.backgroundColor = [UIColor whiteColor];
    
    NSLog(@"%s-- %d",__FUNCTION__,__LINE__);
    // Do any additional setup after loading the view.
}

//4.视图即将显示
-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];
    NSLog(@"%s",__FUNCTION__);
}

//5. 视图已经出现
-(void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];
    NSLog(@"%s",__FUNCTION__);

}
//6. 视图即将消失
-(void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
    NSLog(@"%s--%d",__FUNCTION__,__LINE__);
}
//7. 视图已经消失
-(void)viewDidDisappear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    NSLog(@"%s--%d",__FUNCTION__,__LINE__);
}
//8. 视图消失
- (void)dealloc
{
    NSLog(@"%s--%d",__FUNCTION__,__LINE__);
}

// 模拟器-->硬件-->内存警告

// 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // 之前的写法
    if ([self isViewLoaded] == YES && nil == self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}

上一篇下一篇

猜你喜欢

热点阅读