ios控制器生存週期

  • iOS中控制器的生命週期

  通常咱們在建立控制器的時候,有三種方法。app

  1.  直接經過代碼建立this

  2.  經過storyboard建立spa

  3.  經過Xib,在建立控制器的時候傳入一個Xib文件做爲這個控制器的view。code

 

  • 直接經過代碼建立

  經過代碼建立這種凡是,咱們打印調用順序能夠發現blog

  

  對應的代碼調用順序就是  loadView -> viewDidLoad -> viewWillAppear -> viewWillLayoutSubviews -> viewDidLayoutSubviews -> viewDidAppear生命週期

  

// 1. 
- (void)loadView; // This is where subclasses should create their custom view 
hierarchy if they aren't using a nib. Should never be called directly.

// 2.
- (void)viewDidLoad; // Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set.

// 3.
- (void)viewWillAppear:(BOOL)animated;    // Called when the view is about to made visible. Default does nothing

// 4.
- (void)viewWillLayoutSubviews NS_AVAILABLE_IOS(5_0);
// Called just after the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a nop.

// 5.
- (void)viewDidLayoutSubviews NS_AVAILABLE_IOS(5_0);

  控制器建立以後,彈出另外一個控制器(當前控制器消失),執行的代碼順序 viewWillDisappear -> viewWillLayoutSubviews -> viewDidLayoutSubviews -> viewDidDisappearit

  

  

// 1.
- (void)viewWillDisappear:(BOOL)animated; // Called when the view is dismissed, covered or otherwise hidden. Default does nothing

// 2.
- (void)viewWillLayoutSubviews NS_AVAILABLE_IOS(5_0);
// Called just after the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a nop.

// 3.
- (void)viewDidLayoutSubviews NS_AVAILABLE_IOS(5_0);

// 4.
- (void)viewDidDisappear:(BOOL)animated;  // Called after the view was dismissed, covered or otherwise hidden. Default does nothing

 

  • 經過storyboard建立控制器

  

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:NSStringFromClass([CViewController class]) bundle:nil];
CViewController *touch = [storyBoard instantiateViewControllerWithIdentifier:NSStringFromClass([CViewController class])];
[self presentViewController:touch animated:YES completion:nil];

  爲了方便,同時減小手誤,這裏重用ID咱們使用類名io

   

  經過打印來觀察class

  

  能夠看到代碼的調用順序就是 awakeFromNib -> loadView -> viewWillAppear -> viewWillLayoutSubviews -> viewDidLayoutSubviews -> viewDidAppearsed

// 1.
- (void)awakeFromNib NS_REQUIRES_SUPER;

// 2.
- (void)loadView; // This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly.

// 3.
- (void)viewDidLoad; // Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set.

// 4.
- (void)viewWillAppear:(BOOL)animated;    // Called when the view is about to made visible. Default does nothing

// 5.
- (void)viewWillLayoutSubviews NS_AVAILABLE_IOS(5_0);
// Called just after the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a nop.
- (void)viewDidLayoutSubviews NS_AVAILABLE_IOS(5_0);

// 6.
- (void)viewDidAppear:(BOOL)animated;     // Called when the view has been fully transitioned onto the screen. Default does nothing

  這裏須要注意的是,咱們是控制器經過storyboard來加載,那麼是根據storyboard的描述建立view。

  

  • 經過XIB來建立加載控制器

  首先建立一個XIB,快捷鍵 command + N 來建立。

  

  

  建立後XIB以後咱們還須要設置文件

  

    這裏設置爲咱們建立的控制器

  

  全部須要的已經設置好,這時候咱們能夠來碼代碼了。

DViewController *touch = [[DViewController alloc] initWithNibName:NSStringFromClass([DViewController class]) bundle:nil];
[self presentViewController:touch animated:YES completion:nil];

  下面咱們來觀察控制器的生命週期

  代碼層面上就是

// 1.
- (void)loadView; // This is where subclasses should create their custom view hierarchy if they aren't using a nib. Should never be called directly.

// 2.
- (void)viewDidLoad; // Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set.

// 3.
- (void)viewWillAppear:(BOOL)animated;    // Called when the view is about to made visible. Default does nothing

// 4.可能會屢次調用
- (void)viewWillLayoutSubviews NS_AVAILABLE_IOS(5_0);
// Called just after the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a nop.
- (void)viewDidLayoutSubviews NS_AVAILABLE_IOS(5_0);

// 5.
- (void)viewDidAppear:(BOOL)animated;     // Called when the view has been fully transitioned onto the screen. Default does nothing

  說明:

  加載XIB的時候,咱們一樣能夠不指定XIB名稱,這樣loadView 就會最終加載與控制器同名的 XIB (命名規範很重要啊)

DViewController *touch = [[DViewController alloc] init];
[self presentViewController:touch animated:YES completion:nil];

  同時,若是咱們刪掉 DViewController.xib ,同時建立一個 DView.xib 當咱們用以上不指定xib名稱,一樣是能夠加載出來的(命名規範很重要)。

  咱們能夠知道,加載xib的過程是,若是指定名稱,那麼加載指定名稱的xib,若是沒有指定名稱就會加載和控制器同名的xib,若是沒有就會找去控制器名相同可是沒有 controller 的xib來加載。

 

  • A present B控制器再返回,兩個控制器的生命週期

  1. 建立A控制器

  

  流程:

A:loadView
A:viewDidLoad
A:viewWillAppear
A:viewWillLayout...
A:viewDidAppear

  2. A present 到B控制器

  

  流程:

A:loadView
A:viewDidLoad
A:viewWillAppear
A:viewWillLayout...    // 可能屢次調用
A:viewDidAppear

-------------- Present B控制器 --------------

B:loadView
B:viewDidLoad

A:viewWillDisappear

B:viewWillAppear
B:viewLayout....   // 可能屢次調用

A;viewLayout...   // 可能屢次調用

B:viewDidAppear   // 屢次調用,會跟下面的調用順序可能會有些調換

A:viewDidDisappear
相關文章
相關標籤/搜索