解釋:
UIViewController 視圖控制器 铺根,是其他控制器的父類,可以添加子控制器嵌套使用乔宿。
屬于MVC設(shè)計模式中得C位迂,獲取M數(shù)據(jù),把數(shù)據(jù)傳到V上面。
管理內(nèi)部(整個屏幕)各個view的加載顯示和卸載掂林,同時負(fù)責(zé)與其他ViewController的通信和協(xié)調(diào)臣缀。
分類:
一類主要用于展示內(nèi)容:
比如UIViewController、UITableViewController等泻帮,同時還可以自定義繼承自UIViewController的ViewController精置;
另一類是ViewController容器:
UINavigationController和UITabBarController等,UINavigationController是以Stack的形式來存儲和管理ViewController的锣杂,UITabBarController是以Array的形式來管理ViewController的脂倦。
不管是哪類ViewController,都繼承自UIViewController
創(chuàng)建方式:
1 代碼
2 xib
3 stroyBoard
關(guān)聯(lián):
通常通過 UINavigationController配合來切換頁面。
設(shè)置根控制器:
//在 AppDelegate.m 中寫入:
ViewController1* vc = [[ViewController1 alloc] init];
//導(dǎo)航控制器
UINavigationController* nc = [[UINavigationController alloc] initWithRootViewController:vc];
self.window.rootViewController = nc; //根控制器
//程序自動顯示ViewController1文件顯示的內(nèi)容元莫。
控制器跳轉(zhuǎn)/切換赖阻、返回:
//在 ViewController1 的事件中寫入:
ViewController2* vc2 = [[ViewController2 alloc] init];
[self.navigationController pushViewController:vc2 animated:YES];//(入棧)
//事件執(zhí)行時 自動轉(zhuǎn)到ViewController2文件中。
//返回上一級controller
popViewControllerAnimated: // (出棧)
//返回根controller
popToRootViewControllerAnimated:
兩個頁面?zhèn)髦担?/p>
//1 可以利用NSUserDefaults踱蠢。
//2 實例化一個頁面直接給他的屬性賦值:
ViewController2* vc2 = [[ViewController2 alloc] init];
vc2.vc1 = self;//這個要在2中聲明1是一個類 @class ViewController1; 設(shè)置成員變量火欧,就可以在2中賦值1
vc2.view.backgroundColor = [UIColor purpleColor];
vc2.str = @"MC";
生命周期、加載過程:
load->initialize->init(initWithNibName)—>loadView—>viewDidLoad—>viewWillApper—>viewDidApper—>viewWillDisapper—>viewDidDisapper—>viewWillUnload->viewDidUnload—>dealloc
其中viewWillUnload跟viewDidUnLoad 在iOS6以后就過期了.
LoadView: 控制器調(diào)用loadView方法創(chuàng)建控制器的view.當(dāng)控制器的view存在了就不會調(diào)用.
不要再LoadView中調(diào)用[super loadView],會影響CPU性能
dealloc: 視圖被銷毀茎截,此處需要對你在init和viewDidLoad中創(chuàng)建的對象進行釋放
在這個方法內(nèi),我們通常會把添加在控制器內(nèi)的一些東西給移出.例如:KVO監(jiān)聽者,HUD,定時器等
//視圖已經(jīng)加載 做一些控件的初始化操作,比如給控件設(shè)置尺寸位置苇侵,只在加載的時候調(diào)用一次
- (void)viewDidLoad
//視圖將要出現(xiàn)的時候
- (void)viewWillAppear:(BOOL)animated
//視圖已經(jīng)出現(xiàn)的時候會調(diào)用
- (void)viewDidAppear:(BOOL)animated
//視圖將要消失的時候調(diào)用
- (void)viewWillDisappear:(BOOL)animated
//視圖已經(jīng)消失的時候調(diào)用
- (void)viewDidDisappear:(BOOL)animated
//控件改變位置時調(diào)用,多用于橫豎屏切換企锌。UIScrollView滾動也會觸發(fā)
- (void)viewWillLayoutSubviews
- (void)viewDidLayoutSubviews
//在內(nèi)存不夠的情況下調(diào)用 收到low-memory時系統(tǒng)不會釋放view榆浓,而只是釋放controller的resource。
-(void)didReceiveMemoryWarning
設(shè)置title:
UIViewController *vc;
vc.title = title; // 相當(dāng)于同時設(shè)置了tabBarItem.title和navigationItem.title
vc.tabBarItem.title = title; // tabBar標(biāo)簽上顯示的文字
vc.navigationItem.title = title; // 導(dǎo)航欄上顯示的文字
vc.hidesBottomBarWhenPushed = YES;
// 當(dāng)該控件器被push顯示出來的時候撕攒,自動隱藏底部tabBar哀军;
跳轉(zhuǎn)頁面卡頓解決:
//把當(dāng)前控制器背景顏色設(shè)為目標(biāo)控制器顏色。
vc.view.backgroundColor = [UIColor redColor];
注意事項1:
//不應(yīng)該這樣用4蛉础!
viewController.view.bounds = CGRectMake(50, 50, 100, 200);
[viewController.view addSubview:someOtherViewController.view];
Each custom view controller object you create is responsible for managing all of the views in a single view hierarchy. In iPhone applications, the views in a view hierarchy traditionally cover the entire screen, but in iPad applications they may cover only a portion of the screen. The one-to-one correspondence between a view controller and the views in its view hierarchy is the key design consideration. You should not use multiple custom view controllers to manage different portions of the same view hierarchy. Similarly, you should not use a single custom view controller object to manage multiple screens worth of content.
一個ViewController應(yīng)該且只應(yīng)該管理一個view hierarchy谎倔,而通常來說一個完整的view hierarchy指的是整整占滿的一個屏幕柳击。而很多app滿屏中會有各個區(qū)域分管不同的功能,一些開發(fā)者喜歡直接新建一個ViewController和一套相應(yīng)的View來完成所要的功能(比如我自己=_=)片习。雖然十分方便捌肴,但是卻面臨很多風(fēng)險..
最后附圖: