关于UIViewController的生命周期详解

既然要研究UIViewController的生命周期, 先写一段代码, 我们从代码的执行顺序来了解UIViewController的生命周期
首先 要知道一个UIViewController中都会执行那些方法
1. - (void)loadView
2. - (void)viewDidLoad
3. - (void)viewWillAppear:(BOOL)animated
4. - (void)viewDidAppear:(BOOL)animated
5. - (void)viewWillDisappear:(BOOL)animated
6. - (void)viewDidDisappear:(BOOL)animated
在一段程序执行过程中 按以上顺序依次执行
下面 我们书写代码 运行程序 来看看 首先创建一个 ViewController
- (void)loadView {
[super loadView];
NSLog(@"%@------%s",[self class], FUNCTION);
}

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"%@------%s",[self class], __FUNCTION__);
self.view.backgroundColor = [UIColor redColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(barClick:)];
}

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"%@------%s",[self class], __FUNCTION__);
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"%@------%s",[self class], __FUNCTION__);
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(@"%@------%s",[self class], __FUNCTION__);
}

- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
NSLog(@"%@------%s",[self class], __FUNCTION__);
}
关于UIViewController的生命周期详解_第1张图片
62114635-2C6A-435C-817F-F35C96D17FFA.png

由图可知程序的运行顺序是:

  1. loadView

  2. viewDidLoad

  3. viewWillAppear

  4. viewDidAppear
    这是只有一个页面的情况 如果在这个页面上放一个button push到下一个页面 这是程序执行顺序又会是怎么样的呢
    下面让我们再看看程序的运行结果

     - (void)loadView {
    [super loadView];
    NSLog(@"%@------%s",[self class], __FUNCTION__);
    }
    
    - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    NSLog(@"%@------%s",[self class], __FUNCTION__);
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.leftBarButtonItem =   [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(barClick:)];
    }
    
    - (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"%@------%s",[self class], __FUNCTION__);
     }
    
     - (void)viewDidAppear:(BOOL)animated {
     [super viewDidAppear:animated];
     NSLog(@"%@------%s",[self class], __FUNCTION__);
     }
    
     - (void)viewWillDisappear:(BOOL)animated {
     [super viewWillDisappear:animated];
     NSLog(@"%@------%s",[self class], __FUNCTION__);
     }
    
     - (void)viewDidDisappear:(BOOL)animated {
     [super viewDidDisappear:animated];
     NSLog(@"%@------%s",[self class], __FUNCTION__);
     }  
    
     #pragma mark - buttonClick
     - (void)barClick:(UIBarButtonItem *)button {
     FirstViewController *VC = [[FirstViewController   alloc] init];
     [self.navigationController   pushViewController:VC animated:YES];
     }
    
    
    
     - (void)loadView {
     [super loadView];
     NSLog(@"%@------%s",[self class], __FUNCTION__);
     }
    
     - (void)viewDidLoad {
     [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
    

    NSLog(@"%@------%s",[self class], FUNCTION);
    self.view.backgroundColor = [UIColor greenColor];

    }

    // 视图将要出现时就会调用

    • (void)viewWillAppear:(BOOL)animated {
      [super viewWillAppear:animated];
      NSLog(@"%@------%s",[self class], FUNCTION);
      }
      // 视图完全出现时就会调用
    • (void)viewDidAppear:(BOOL)animated {
      [super viewDidAppear:animated];
      NSLog(@"%@------%s",[self class], FUNCTION);
      }
      // 视图将要消失会调用
    • (void)viewWillDisappear:(BOOL)animated {
      [super viewWillDisappear:animated];
      NSLog(@"%@------%s",[self class], FUNCTION);
      }
      // 视图完全消失会调用
    • (void)viewDidDisappear:(BOOL)animated {
      [super viewDidDisappear:animated];
      NSLog(@"%@------%s",[self class], FUNCTION);
      }
关于UIViewController的生命周期详解_第2张图片
4A3CCD27-6838-4F94-BEEF-511FCECB79FA.png

在ViewController页面中有一个button 点击button可以push到FirstViewController 由程序运行结果可知: 当刚运行程序时按顺序依次执行:

  1. loadView
  2. viewDidLoad
  3. viewWillAppear
  4. viewDidAppear
  5. viewWillDisappear
  6. FirstViewController loadView
  7. FirstViewController viewDidLoad
  8. FirstViewController viewWillAppear
  9. viewController viewDidDisappear
  10. FirstViewController viewDidappear

当再一次点击左上角的返回键时 从FirstViewController跳转到viewController 由程序的运行结果可知 程序的运行顺序为;

  1. loadView
  2. viewDidLoad
  3. viewWillAppear
  4. viewDidAppear
  5. viewWillDisappear
  6. FirstViewController loadView
  7. FirstViewController viewDidLoad
  8. FirstViewController viewWillAppear
  9. viewController viewDidDisappear
  10. FirstViewController viewDidappear
  11. FirstViewController viewWillDisappear
  12. viewController viewWillAppear
  13. FirstViewController viewWillDisappear
  14. viewController viewDidAppear


    关于UIViewController的生命周期详解_第3张图片
    8037D62C-4A2C-4CB3-9289-D705418B2839.png

从程序的执行顺序可以看出来每个viewController只执行一次loadView和viewDidLoad

你可能感兴趣的:(关于UIViewController的生命周期详解)