16/08/04/wed
iOS視圖跳轉的方式
1.使用modal方式進行跳轉
modal方式跳轉,其實就是通過方法 - (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion跳轉
present方法,是所有視圖控制器對象都有的成員方法,但是需要注意的是,同一個視圖控制器,在同一個時間,只能present一個另外的視圖控制器,如果當前的VC已經present了,再次present一個VC時,就會提示失敗,如果想繼續present,就必須將原來present的控制器dismiss。
另外有兩個只讀屬性: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;modal方法一般用在兩個控制器沒有什么邏輯關系的情況下,且彈出方式是從下往上彈出,彈出另一個控制器的時候自身并不會銷毀
-(void)login{
KCLoginViewController *loginVC=[[KCLoginViewController alloc]init];
//調用此方法顯示窗口
[self presentViewController:loginVC animated:YES completion:nil];
}
===============================================================================
//如果想要在modal下一個控制器的時候銷毀當前控制器,可以把主窗口的根控制器設置為modal 出的控制器,代碼如下:
BViewController *bvc = [[BViewController alloc] init];
[self presentViewController:bvc animated:YES completion:nil];
[UIApplication sharedApplication].keyWindow.rootViewController = bvc;
2.使用導航控制器進行跳轉
導航控制器進行界面跳轉用的方法為:pushViewController,且此種方法是uinavigationcontroller 和其子類才有的方法
彈出某個界面,返回到上個界面使用方法為:popViewController(注意,當當前界面是根結面時,這個方法是不起作用的)
跳轉到某個特定的控制器,使用的方法為:popToViewController(獲取需要跳轉的界面的方式有很多,我一般是遍歷UINavigationController的viewControllers數組,用iskindofclass方法來獲取某個控制器對象再來跳轉的)
導航控制器中的子控制器以棧的方式進行管理,各個視圖的切換就是壓棧和出棧操作,出棧后的視圖會立即銷毀,且彈出方式為從右到左
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
_window.backgroundColor =[UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1];
KCFriendViewController *friendVC=[[KCFriendViewController alloc]init];
UINavigationController *navigationController=[[UINavigationController alloc]initWithRootViewController:friendVC];
_window.rootViewController=navigationController;
[_window makeKeyAndVisible];
return YES;
3.通過UITabBarcontroller進行跳轉
一般作為app的根界面視圖控制器。UITabBarController的界面跳轉,其實是界面切換,因為UITabBarController的界面跳轉其實就是UITabBarController的viewControllers數組中的幾個界面切換
UITabBarController:以平行的方式管理視圖,各個視圖之間往往關系并不大,在初始化UITabBarController時,每個加入到UITabBarController的視圖都會進行初始化即使當前不顯示在界面上。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
_window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
KCTabBarViewController *tabBarController=[[KCTabBarViewController alloc]init];
KCWebChatViewController *webChatVC=[[KCWebChatViewController alloc]init];
KCContactViewController *contactVC=[[KCContactViewController alloc]init];
tabBarController.viewControllers=@[webChatVC,contactVC];
//注意默認情況下UITabBarController在加載子視圖時是懶加載的,所以這里調用一次contactVC,否則在第一次展示時只有第一個控制器tab圖標,contactController的tab圖標不會顯示
for (UIViewController *controller in tabBarController.viewControllers) {
UIViewController *view= controller.view;
}
_window.rootViewController=tabBarController;
[_window makeKeyAndVisible];
return YES;
}