一、模態視圖
視圖切換,純代碼的情況下,沒有NavigationController,一般會用到presentViewController來切換視圖并攜帶切換時的動畫。
其中切換方法如下:
– presentViewController:animated:completion: 彈出,出現一個新視圖 可以帶動畫效果,完成后可以做相應的執行函數經常為nil
– dismissViewControllerAnimated:completion:退出一個新視圖 可以帶動畫效果,完成后可以做相應的執行函數經常為nil
切換動畫在壓入一個新視圖和彈出頂層視圖均可以使用。
利用模態視圖進行多個頁面跳轉后,要返回最初始的頁面則需要了解到控制器的兩個屬性presentedViewController和presentingViewController,他們分別是被present的控制器和正在presenting的控制器。比如說, 控制器A和B,[A presentViewController B animated:YES completion:nil]; 那么A相對于B就是presentingViewController,B相對于A是presentedViewController,即這個時候
B.presentingViewController = A;
A.presentedViewController = B;
利用模態跳轉,從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的。
二、導航控制器UINavigationController
UINaviGationController通常被我們稱為導航欄,他是視圖與視圖之間聯系溝通的橋梁,切換方法如下:
推出某個視圖控制器
[self.navigationController pushViewController:viewController animated:YES]; ? ?
UINavigationController是一個視圖控制器的容器,他里面可能放了很多個控制器,所以返回的時候可以分為幾種情況。
1、彈出當前顯示的界面,返回到上個界面(注意,當當前界面是根界面時,這個方法是不起作用的)
[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];
三、選項卡UITabBarController控制器
其實與其說UITabBarController的界面跳轉,不如說是界面切換,因為UITabBarController的界面跳轉其實就是UITabBarController的viewControllers數組中的幾個界面切換。通過調用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" ];
// 添加子控制器(這些子控制器會自動添加到UITabBarController的 viewControllers 數組中)
[tabbarVC addChildViewController :oneVC];
[tabbarVC addChildViewController :twoVC];
四、Storyboard的segues方式跳轉
此方法僅適用于Storyboard中各個頁面連線后的跳轉,鼠標點擊viewControlller,按住control鍵拖拽到另一個View頁面,在彈出的segue頁面中選擇跳轉模式即可,連線完之后選中連線,在Identifier填上對應的標示,然后再在需要跳轉的地方實現如下代碼即可:
[self performSegueWithIdentifier:@"test" sender:self];
如果連線的方式是push,則ViewController需要由UINavigationController來管理,返回方式則和UINavigationController一樣
如果連線的方式是model,則ViewController不需要由UINavigationController來管理,返回方式和模態的返回方式一樣
如果連線的方式是custom,則需要自定義segue,自定義segue在此不討論。
參考資料:http://blog.csdn.net/ityanping/article/details/39270609
http://www.mamicode.com/info-detail-469709.html
ps:本人接觸ios時間不是很長,寫文章的目的是為了和大家分享,另外一個是促進自己進步,適合初中級開發人員觀看,有什么不足的地方,歡迎大家指正。此文章是在別人的基礎上增加自己的理解,如有侵權的地方,請聯系本人修改。