主題思想:如A、B、C、D 四個視圖控制器
想要在 A push B 后, B 在push 到 D ,然后從 D pop 到 C ,在從 C pop 的A
解決方法如下:
1.假如此時在 A 控制器下,想要到 push 到 B, 可以這樣寫
[self.navigationController pushViewController: B :YES];
這時 self.navigationController.viewControllers 中按順序含有 [A,B]
2.此時已經(jīng)到 B 控制器下了, 接下來是 push 到 D, 可以這樣寫
[self.navigationController pushViewController: D :YES];
這時 self.navigationController.viewControllers 中按順序含有 [A,B,D]
接下來很重要,很重要,很重要:
如何想從 D pop 到 C, 看數(shù)組[A,B,D] 中壓根就沒有C 該如何pop 到C呢?
這時就需要對這個數(shù)組進行修改,將C 加入進去
于是 你會如下寫:
[self.navigationController.viewControllers addObject:C];
發(fā)現(xiàn)報錯,這是因為self.navigationController.viewControllers 是不可變數(shù)組,沒辦法了,我們只好轉(zhuǎn)換一下了:
NSMutableArray*tempMarr =[NSMutableArrayarrayWithArray:self.navigationController.viewControllers];
此時再加入C 就容易多了,咦,聰明的你會發(fā)現(xiàn)從 D pop C 不能直接將 C直接 addObject;
當然,我會這樣做:
[tempMarr insertObject:C atIndex:tempMarr.count- 2];
這時候 tempMarr 是這樣的 [A,B,C,D], 可是 要想 從 C pop 到 A ,數(shù)組中就不能有 B
就需要 將tempMarr 變成 [A,C,D] ,至于怎么變,你比我懂得多,
懂得思考的同學會發(fā)現(xiàn) 這時的self.navigationController.viewControllers 依然是 [A,B,D], 不用急,不用怕navigationController 有這樣一個方法, 可以搞定,如下:
[self.navigationController setViewControllers:tempMarr animated:YES];
有的同學會說,這不是直接把 B 替換 成 C 嗎
看上去是這樣,可是跳轉(zhuǎn)的時機,時機,時機重要的事情說三遍,還有視圖的切換,切換,切換
此時還在 B 控制器中,這些處理過程都是在 B 中處理的 , 也必須是 B 執(zhí)行了 push 到 D 方法后,也就是說
[self.navigationController pushViewController:D animated:YES];
之后 進行的 數(shù)組處理;
附加代碼:
在B 控制器中處理:
-(void)pushTest {
[self.navigationController pushViewController:D animated:YES];
NSMutableArray*tempMarr =[NSMutableArrayarrayWithArray:self.navigationController.viewControllers];
[tempMarr insertObject:C atIndex:tempMarr.count- 2];
[tempMarr removeObject:self]; //此時 的self 就是指 B ,因為在 B 中呢
[self.navigationController setViewControllers:tempMarr animated:YES];
}?