ios控制器生存周期

來源:互聯網
上載者:User

標籤:cal   self   lap   necessary   views   過程   appear   dismiss   hive   

  • iOS中控制器的生命週期

  一般我們在建立控制器的時候,有三種方法。

  1.  直接通過代碼建立

  2.  通過storyboard建立

  3.  通過Xib,在建立控制器的時候傳入一個Xib檔案作為這個控制器的view。

 

  • 直接通過代碼建立

  通過代碼建立這種凡是,我們列印調用順序可以發現

  

  對應的代碼調用順序就是  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 -> viewDidDisappear

  

  

// 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我們使用類名

   

  通過列印來觀察

  

  可以看到代碼的調用順序就是 awakeFromNib -> loadView -> viewWillAppear -> viewWillLayoutSubviews -> viewDidLayoutSubviews -> viewDidAppear

// 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:loadViewA:viewDidLoadA:viewWillAppearA:viewWillLayout...A:viewDidAppear

  2. A present 到B控制器

  

  流程:

A:loadViewA:viewDidLoadA:viewWillAppearA:viewWillLayout...    // 可能多次調用A:viewDidAppear-------------- Present B控制器 --------------B:loadViewB:viewDidLoadA:viewWillDisappearB:viewWillAppearB:viewLayout....   // 可能多次調用A;viewLayout...   // 可能多次調用B:viewDidAppear   // 多次調用,會跟下面的調用順序可能會有些調換A:viewDidDisappear

 

ios控制器生存周期

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.