//
// SecondViewController.h
// IOS150702_UI(03)_UIViewController生命周期
//
// Created by PengJunlong on 15/7/2.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@end
//
// SecondViewController.m
// IOS150702_UI(03)_UIViewController生命周期
//
// Created by PengJunlong on 15/7/2.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//
#import "SecondViewController.h"
#import "ViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)loadView
{
[super loadView]; //都要先调用父类的对应方法
NSLog(@"第二个:开始加载视图");
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"第二个:视图加载完成");
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(50, 100, self.view.frame.size.width-100, 50);
button.backgroundColor = [UIColor redColor];
[button setTitle:@"视图跳转" forState:UIControlStateNormal];
[button addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
self.view.backgroundColor = [UIColor blueColor];
}
- (void)btnClicked
{
//使消失当前视图控制器视图
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"第二个:视图将要显示,还没有显示");
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"第二个:视图已显示");
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSLog(@"第二个:视图将要消失,还没有消失");
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
NSLog(@"第二个:视图已消失");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// ViewController.m
// IOS150702_UI(03)_UIViewController生命周期
//
// Created by PengJunlong on 15/7/2.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//
#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
@end
//视图控制器的生命周期
//当一个视图控制器被创建,并在屏幕上显示的时候。 代码的执行顺序
//1、 alloc 创建对象,分配空间
//2、init (initWithNibName) 初始化对象,初始化数据
//3、loadView 从nib载入视图 ,通常这一步不需要去干涉。除非你没有使用xib文件创建视图
//4、viewDidLoad 载入完成,可以进行自定义数据以及动态创建其他控件
//5、viewWillAppear 视图将出现在屏幕之前,马上这个视图就会被展现在屏幕上了
//6、viewDidAppear 视图已在屏幕上渲染完成
//当一个视图被移除屏幕并且销毁的时候的执行顺序,这个顺序差不多和上面的相反
//1、viewWillDisappear 视图将被从屏幕上移除之前执行
//2、viewDidDisappear 视图已经被从屏幕上移除,用户看不到这个视图了
//3、dealloc 视图被销毁,此处需要对你在init和viewDidLoad中创建的对象进行释放
@implementation ViewController
- (void)loadView
{
[super loadView]; //都要先调用父类的对应方法
NSLog(@"第一个:开始加载视图");
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"第一个:视图加载完成");
// Do any additional setup after loading the view, typically from a nib.
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(50, 100, self.view.frame.size.width-100, 50);
button.backgroundColor = [UIColor redColor];
[button setTitle:@"视图跳转" forState:UIControlStateNormal];
[button addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
self.view.backgroundColor = [UIColor yellowColor];
}
- (void)btnClicked
{
//设置翻页效果
//typedef NS_ENUM(NSInteger, UIModalTransitionStyle) {
// UIModalTransitionStyleCoverVertical = 0,
// UIModalTransitionStyleFlipHorizontal,
// UIModalTransitionStyleCrossDissolve,
// UIModalTransitionStylePartialCurl NS_ENUM_AVAILABLE_IOS(3_2),
//};
SecondViewController *svc = [[SecondViewController alloc] init];
svc.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentViewController:svc animated:YES completion:^{}];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"第一个:视图将要显示,还没有显示");
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"第一个:视图已显示");
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
NSLog(@"第一个:视图将要消失,还没有消失");
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
NSLog(@"第一个:视图已消失");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
2015-07-02 18:44:34.219 IOS150702_UI(03)_UIViewController生命周期[1389:57933] 第一个:开始加载视图
2015-07-02 18:44:34.220 IOS150702_UI(03)_UIViewController生命周期[1389:57933] 第一个:视图加载完成
2015-07-02 18:44:34.220 IOS150702_UI(03)_UIViewController生命周期[1389:57933] 第一个:视图将要显示,还没有显示
2015-07-02 18:44:34.255 IOS150702_UI(03)_UIViewController生命周期[1389:57933] 第一个:视图已显示
点击按钮后的结果:
2015-07-02 18:44:34.219 IOS150702_UI(03)_UIViewController生命周期[1389:57933] 第一个:开始加载视图
2015-07-02 18:44:34.220 IOS150702_UI(03)_UIViewController生命周期[1389:57933] 第一个:视图加载完成
2015-07-02 18:44:34.220 IOS150702_UI(03)_UIViewController生命周期[1389:57933] 第一个:视图将要显示,还没有显示
2015-07-02 18:44:34.255 IOS150702_UI(03)_UIViewController生命周期[1389:57933] 第一个:视图已显示
2015-07-02 18:47:45.577 IOS150702_UI(03)_UIViewController生命周期[1389:57933] 第二个:开始加载视图
2015-07-02 18:47:45.577 IOS150702_UI(03)_UIViewController生命周期[1389:57933] 第二个:视图加载完成
2015-07-02 18:47:45.581 IOS150702_UI(03)_UIViewController生命周期[1389:57933] 第一个:视图将要消失,还没有消失
2015-07-02 18:47:45.581 IOS150702_UI(03)_UIViewController生命周期[1389:57933] 第二个:视图将要显示,还没有显示
2015-07-02 18:47:46.185 IOS150702_UI(03)_UIViewController生命周期[1389:57933] 第二个:视图已显示
2015-07-02 18:47:46.185 IOS150702_UI(03)_UIViewController生命周期[1389:57933] 第一个:视图已消失
图: