一、模態(tài)視圖
視圖切換,純代碼的情況下锣枝,沒有NavigationController,一般會(huì)用到presentViewController來切換視圖并攜帶切換時(shí)的動(dòng)畫兰英。
其中切換方法如下:
– presentViewController:animated:completion: 彈出惊橱,出現(xiàn)一個(gè)新視圖 可以帶動(dòng)畫效果,完成后可以做相應(yīng)的執(zhí)行函數(shù)經(jīng)常為nil
– dismissViewControllerAnimated:completion:退出一個(gè)新視圖 可以帶動(dòng)畫效果箭昵,完成后可以做相應(yīng)的執(zhí)行函數(shù)經(jīng)常為nil
切換動(dòng)畫在壓入一個(gè)新視圖和彈出頂層視圖均可以使用。
利用模態(tài)視圖進(jìn)行多個(gè)頁面跳轉(zhuǎn)后回季,要返回最初始的頁面則需要了解到控制器的兩個(gè)屬性presentedViewController和presentingViewController家制,他們分別是被present的控制器和正在presenting的控制器。比如說泡一, 控制器A和B颤殴,[A presentViewController B animated:YES completion:nil]; 那么A相對(duì)于B就是presentingViewController,B相對(duì)于A是presentedViewController鼻忠,即這個(gè)時(shí)候
B.presentingViewController = A;
A.presentedViewController = B;
利用模態(tài)跳轉(zhuǎn)涵但,從A present到B,再從B present到C帖蔓,然后從C present到D矮瘟,最后要從D返回到A,返回過程如下:
-(void)dismissModalStack {
UIViewController *vc = self.presentingViewController;
while (vc.presentingViewController) {
vc = vc.presentingViewController;
}
[vc dismissViewControllerAnimated:YES completion:NULL];
}
presentingViewController返回的是除model外最頂層的ViewController塑娇,而不是彈出model的澈侠。
二、導(dǎo)航控制器UINavigationController
UINaviGationController通常被我們稱為導(dǎo)航欄埋酬,他是視圖與視圖之間聯(lián)系溝通的橋梁哨啃,切換方法如下:
推出某個(gè)視圖控制器
[self.navigationController pushViewController:viewController animated:YES]; ? ?
UINavigationController是一個(gè)視圖控制器的容器烧栋,他里面可能放了很多個(gè)控制器,所以返回的時(shí)候可以分為幾種情況拳球。
1审姓、彈出當(dāng)前顯示的界面,返回到上個(gè)界面(注意祝峻,當(dāng)當(dāng)前界面是根界面時(shí)魔吐,這個(gè)方法是不起作用的)
[self.navigationController popViewControllerAnimated:YES];
2、返回到根視圖控制器
[self.navigationController popToRootViewControllerAnimated:YES];?
3呼猪、彈出到指定視圖控制器
UIViewController *viewController=nil;
for (UIViewController *tempVc in self.navigationController.viewControllers) {
if ([tempVc isKindOfClass:[UIViewController class]]) {
viewController=tempVc;
}
}
[self.navigationController popToViewController:viewController animated:YES];
三画畅、選項(xiàng)卡UITabBarController控制器
其實(shí)與其說UITabBarController的界面跳轉(zhuǎn),不如說是界面切換宋距,因?yàn)閁ITabBarController的界面跳轉(zhuǎn)其實(shí)就是UITabBarController的viewControllers數(shù)組中的幾個(gè)界面切換轴踱。通過調(diào)用UITabBarController的addChildViewController方法添加子控制器:
UITabBarController *tabbarVC = [[ UITabBarController alloc ] init ];
OneViewController *oneVC = [[OneViewController ] init ];
oneVC.tabBarItem.title = @"one" ;
oneVC.tabBarItem.image = [ UIImage imageNamed : @"one.png" ];
TwoViewController *twoVC = [[TwoViewController ] init ];
twoVC.tabBarItem.title = @"two" ;
twoVC. tabBarItem.image = [UIImage imageNamed : @"two.png" ];
// 添加子控制器(這些子控制器會(huì)自動(dòng)添加到UITabBarController的 viewControllers 數(shù)組中)
[tabbarVC addChildViewController :oneVC];
[tabbarVC addChildViewController :twoVC];
四、Storyboard的segues方式跳轉(zhuǎn)
此方法僅適用于Storyboard中各個(gè)頁面連線后的跳轉(zhuǎn)谚赎,鼠標(biāo)點(diǎn)擊viewControlller淫僻,按住control鍵拖拽到另一個(gè)View頁面,在彈出的segue頁面中選擇跳轉(zhuǎn)模式即可壶唤,連線完之后選中連線雳灵,在Identifier填上對(duì)應(yīng)的標(biāo)示,然后再在需要跳轉(zhuǎn)的地方實(shí)現(xiàn)如下代碼即可:
[self performSegueWithIdentifier:@"test" sender:self];
如果連線的方式是push闸盔,則ViewController需要由UINavigationController來管理悯辙,返回方式則和UINavigationController一樣
如果連線的方式是model,則ViewController不需要由UINavigationController來管理迎吵,返回方式和模態(tài)的返回方式一樣
如果連線的方式是custom躲撰,則需要自定義segue,自定義segue在此不討論击费。
參考資料:http://blog.csdn.net/ityanping/article/details/39270609
http://www.mamicode.com/info-detail-469709.html
ps:本人接觸ios時(shí)間不是很長(zhǎng)拢蛋,寫文章的目的是為了和大家分享,另外一個(gè)是促進(jìn)自己進(jìn)步蔫巩,適合初中級(jí)開發(fā)人員觀看谆棱,有什么不足的地方,歡迎大家指正圆仔。此文章是在別人的基礎(chǔ)上增加自己的理解垃瞧,如有侵權(quán)的地方,請(qǐng)聯(lián)系本人修改荧缘。