一:首先瞭解一下生命周期圖 二:UIViewController 生命周期介紹 1.通過alloc init 分配記憶體,初始化controller. 2.loadView loadView方法預設實現[super loadView] 如果在初始化controller時指定了xib文件名,就會根據傳入 ...
一:首先瞭解一下生命周期圖
二:UIViewController 生命周期介紹
1.通過alloc init 分配記憶體,初始化controller.
2.loadView
loadView方法預設實現[super loadView]
如果在初始化controller時指定了xib文件名,就會根據傳入的xib文件名載入對應的xib文件,如果沒傳xib文件名,預設會載入跟controller同名的xib文件,如果沒找到相關聯的xib文件,就會創建一個空白的UIView,然後賦給controller的view
3.viewDidLoad
當loadView創建完view之後,此時view已經完成載入了,會調用viewDidLoad方法;一般我會在這裡做界面上的初始化操作,比如添加按鈕,子視圖,等等.
4.viewWillAppear
當view在load完之後,將要顯示在屏幕之前會調用這個方法
在重寫這些方法時候最好先調用一下系統的方法之後在做操作。
5.viewDidAppear
當view已經在屏幕上顯示出來之後,會調用這個方法
當一個視圖被移除屏幕並且銷毀的時候
6.viewWillDisappear
當視圖將要從屏幕上移除時候調用
7.viewDidDisappear
當視圖已經從屏幕上移除時候調用
8.dealloc
view被銷毀時候調用,如果是手動管理記憶體的話,需要釋放掉之前在init和viewDidLoad中分配的記憶體(類似alloc,new,copy);dealloc方法不能由我們主動調用,必須等引用計數為0時候由系統調用.
註意:viewDidUnload 在6.0之後已經廢棄了。簡單來說,對於iOS6,你不需要做任何以前viewDidUnload的事情,更不需要把以前viewDidUnload的代碼移動到 didReceiveMemoryWarning方法中
三:UIViewController 生命周期實例
首先創建一個 ViewController,並這個頁面上放一個button push到下一個頁面;查看它的生命周期執行順序;
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__); } #pragma mark - buttonClick - (void)barClick:(UIBarButtonItem *)button { FirstViewController *VC = [[FirstViewController alloc] init]; [self.navigationController pushViewController:VC animated:YES]; }
第二個控制器FirstViewController 代碼:
- (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__); }
在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
從程式的執行順序可以看出來每個viewController只執行一次loadView和viewDidLoad