ViewController是IOS開發中MVC模式中的C,ViewController是view的controller,ViewController的職責主要包括管理內部各個view的加載顯示和卸載,同時負責與其餘ViewController的通訊和協調。html
在IOS中,有兩類ViewController,一類是顯示內容的,好比UIViewController、UITableViewController等,同時還能夠自定義繼承自UIViewController的ViewController;另外一類是ViewController容器,UINavigationViewController和UITabBarController等,UINavigationController是以Stack的形式來存儲和管理ViewController,UITabBarController是以Array的形式來管理ViewController。ios
能夠看到,在Controller中建立View有2種方法,一種是使用Storyboard等可視化工具,另外一種是經過代碼建立。可視化建立在工程中不多用到,由於它知足不了開發者如飢似渴的需求--。app
1.建立新的Empty Application Projectide
2.新建ViewController的類,添加loadView方法,及viewDidLoad等方法工具
// // XYZViewController.m // ViewLifeCycle // // Created by Norcy on 14-7-28. // Copyright (c) 2014年 QQLive. All rights reserved. // #import "XYZViewController.h" @interface XYZViewController () @end @implementation XYZViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)loadView { UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; contentView.backgroundColor = [UIColor blueColor]; self.view = contentView; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NSLog(@"View Did Load"); } - (void)viewWillAppear:(BOOL)animated { NSLog(@"View Will Appear"); } - (void)viewDidAppear:(BOOL)animated { NSLog(@"View Did Appear"); } - (void)viewWillDisappear:(BOOL)animated { NSLog(@"View Will Disappear"); } - (void)viewDidDisappear:(BOOL)animated { NSLog(@"View Did Disappear"); } @end
3.在AppDelegate.m中的application:didFinishLaunchingWithOptions:中註冊ViewController性能
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; XYZViewController *viewController = [[XYZViewController alloc]initWithNibName:nil bundle:nil]; self.window.rootViewController = viewController; [self.window makeKeyAndVisible]; return YES; }
雖然咱們能夠在AppDelegate.m中的application:didFinishLaunchingWithOptions:中設置window的代碼後面添加view,可是在通常的工程中,咱們不會在委託類中管理咱們的View。 而是利用委託類中的UIWindow去添加UIViewController,再在ViewController類中去管理View。學習
附,建立帶NavigationController的代碼spa
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; /*設置Navigation controller*/ XYZFirstViewController *viewController = [[XYZFirstViewController alloc] initWithNibName:nil bundle:nil]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController]; [nav setNavigationBarHidden:YES animated:NO]; self.window.rootViewController = nav; [self.window makeKeyAndVisible]; [viewController release]; [nav release]; return YES; }
alloc -> initWithNibName -> loadView -> viewDidLoad -> viewWillAppear -> viewDidAppear -> viewWillDisappear -> viewDidDisappear -> dealloc.net
注意viewWillUnload和viewDidUnload已經在ios6被廢棄了,由於Clearing references to views is no longer necessary。code
注意1.沒有viewWillLoad。
注意2.viewDidLoad和viewDidUnload並非成對的。
啓動程序
2014-07-28 17:43:36.124 ViewLifeCycle[4007:a0b] View Did Load
2014-07-28 17:43:36.125 ViewLifeCycle[4007:a0b] View Will Appear
2014-07-28 17:43:36.128 ViewLifeCycle[4007:a0b] View Did Appear
按下Home鍵,並無任何記錄
雙擊Home鍵,刪除該程序
2014-07-28 17:43:51.327 ViewLifeCycle[4007:a0b] View Will Disappear
2014-07-28 17:43:51.327 ViewLifeCycle[4007:a0b] View Did Disappear
爲何按下Home鍵以後沒有調用viewWillDisappear和viewDidDisappear呢?
由於在ios4後引入了後臺的概念,當按下Home鍵以後,程序被掛起了,可是該View依然是原來的View,並非新的。因此只有內存不夠的時候或程序被終止的時候,纔會調用viewWillDisappear和viewDidDisappear。
view的nib文件爲nil時,手工建立視圖界面時調用loadView;當view的nib文件存在的時候,初始化工做在viewDidLoad中實現。
loadView時view尚未生成,viewDidLoad時,view已經生成了。loadView方法用來建立一個新的view。loadView只會被調用一次,而viewDidLoad可能會被調用屢次。
當受到內存警告時,那麼此時系統默認操做會檢查當前視圖控制器的view是否還在使用,若是沒在使用且控制器實現了loadView方法,ViewController會將view release並將其指向爲nil。
注意,不要在loadView中調用父類方法[super loadView],由於這會影響CPU性能。
注意2,切換先後臺不會調用viewWillAppear