標籤:cal self lap necessary views 過程 appear dismiss hive
一般我們在建立控制器的時候,有三種方法。
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
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,快速鍵 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控制器生存周期