for (UIViewController *temp in self.navigationController.viewControllers) {
if ([temp isKindOfClass:[你要跳轉(zhuǎn)到的Controller class]]) {
[self.navigationController popToViewController:temp animated:YES];
}
}
1厨姚、[self.view addSubView:view];和[self.window addSubView:view];需要注意突琳,此方法只是把頁面(view)加在當(dāng)前頁面(view)上,控制器(viewController)還是原來那個控制器。此時再用[self.navigationColler pushViewController:animated:];和 [self.navigationController popViewControllerAnimated:]; 是不行的充坑。要想使用pushViewController和popViewController進(jìn)行視圖間的切換减江,就必須要求當(dāng)前視圖是個NavigationController。
2捻爷、有NavigationController導(dǎo)航欄的話辈灼,使用[self.navigationColler pushViewController:animated:];和[self.navigationController popViewControllerAnimated:];來進(jìn)行視圖切換。pushViewController是進(jìn)入到下一個視圖也榄,popViewController是返回到上一視圖巡莹。
3、沒有NavigationController導(dǎo)航欄的話甜紫,使用[self presentViewController:animated:completion:];和[self dismissViewControllerAnimated:completion:];具體是使用可以從文檔中詳細(xì)了解降宅。
4臭猜、要想使用pushViewController和popViewController來進(jìn)行視圖切換挠进,首先要確保根視圖是NavigationController舀武,不然是不可以用的焊夸。這里提供一個簡單的方法讓該視圖或者根視圖是NavigationController裸违。自己定義個子類繼承UINavigationController住练,然后將要展現(xiàn)的視圖包裝到這個子類中交洗,這樣就可以使NavigationController了梧躺。提供的這個方法有很好的好處劣挫,就是可以統(tǒng)一的控制各個視圖的屏幕旋轉(zhuǎn)册养。將一個控制器(UIViewController)包裝成一個導(dǎo)航控制器(UINavigationController):
UIViewController *vc = [[UIViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
1. 用UINavigationController的時候用
----進(jìn)入下一個視圖[self.navigationColler pushViewController:animated:];
----返回之前的視圖[self.navigationController popViewControllerAnimated:];
----ps:push以后會在navigation的left bar自動添加back按鈕,它的響應(yīng)方法就是返回压固。所以一般不需要寫返回方法球拦,點(diǎn)back按鈕即可。
2. 其他時候(視圖不是UINavigationController的時候帐我,只是一個viewController時)
----進(jìn)入下一個視圖:[self presentViewController:animated:completion:];
----返回之前的視圖:[self dismissViewControllerAnimated:completion:];
3. 切換視圖一般用不到addSubview
UINavigationController是導(dǎo)航控制器刘莹,如果pushViewController的話,會跳轉(zhuǎn)到下一個ViewController焚刚,點(diǎn)返回會回到現(xiàn)在這個ViewController点弯;如果是addSubview的話,其實(shí)還是對當(dāng)前的ViewController操作矿咕,只是在當(dāng)前視圖上面又“蓋”住了一層視圖抢肛,其實(shí)原來的畫面在下面呢,看不到而已碳柱。(當(dāng)然捡絮,也可以用insertSubView? atIndex那個方法設(shè)置放置的層次)。