1、
[self.view addSubView:view];和[self.window addSubView:view];需要注意,此方法只是把頁面(view)加在當前頁面(view)上,控制器(viewController)還是原來那個控制器。此時再用[self.navigationColler pushViewController:animated:];和 [self.navigationController popViewControllerAnimated:]; 是不行的。要想使用pushViewController和popViewController進行視圖間的切換,就必須要求當前視圖是個NavigationController。
2、有NavigationController導航欄的話,使用
[self.navigationColler pushViewController:animated:];和
[self.navigationController popViewControllerAnimated:];來進行視圖切換。pushViewController是進入到下一個視圖,popViewController是返回到上一視圖。
3、沒有NavigationController導航欄的話,使用
[self presentViewController:animated:completion:];和
[self dismissViewControllerAnimated:completion:];具體是使用可以從文檔中詳細了解。
4、要想使用pushViewController和popViewController來進行視圖切換,首先要確保根視圖是NavigationController,不然是不可以用的。這里提供一個簡單的方法讓該視圖或者根視圖是NavigationController。自己定義個子類繼承UINavigationController,然后將要展現的視圖包裝到這個子類中,這樣就可以使NavigationController了。提供的這個方法有很好的好處,就是可以統一的控制各個視圖的屏幕旋轉。將一個控制器(UIViewController)包裝成一個導航控制器(UINavigationController):
UIViewController *vc = [[UIViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
1. 用UINavigationController的時候用
----
進入下一個視圖[self.navigationColler pushViewController:animated:];
----
返回之前的視圖[self.navigationController popViewControllerAnimated:];
----ps:push以后會在navigation的left bar自動添加back按鈕,它的響應方法就是返回。所以一般不需要寫返回方法,點back按鈕即可。
2. 其他時候(視圖不是UINavigationController的時候,只是一個viewController時)
----
進入下一個視圖:[self presentViewController:animated:completion:];
----
返回之前的視圖:[self dismissViewControllerAnimated:completion:];
3.
切換視圖一般用不到addSubview
UINavigationController是導航控制器,如果pushViewController的話,會跳轉到下一個ViewController,點返回會回到現在這個ViewController;如果是addSubview的話,其實還是對當前的ViewController操作,只是在當前視圖上面又“蓋”住了一層視圖,其實原來的畫面在下面呢,看不到而已。(當然,也可以用insertSubView atIndex那個方法設置放置的層次)。
4.另加一個:
使用presentViewControllerAnimated方法從A->B->C,若想在C中直接返回A,則可這樣實現:
C中返回事件:
- void back
- {
- [self dismissViewControllerAnimated:NO];//注意一定是NO!!
- [[NSNotificationCenter defaultCenter]postNotificationName:@"backback" object:nil];
- }
然后在B中,
- //在viewdidload中:
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(back) name:@"backback" object:nil];
- -(void)back
- {
- [self dismissViewControllerAnimated:YES];
- }