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