前言
維護(hù)工程中有個(gè)多層控制器,但當(dāng)要dismissViewControllerAnimated 時(shí)不時(shí) 不起效果。
原理
我們都知道dismissViewControllerAnimated:completion:方法是針對(duì)被present出來(lái)的控制器的,一般我們這樣使用:在一個(gè)控制器中present另外一個(gè)控制器A,然后在A中執(zhí)行dismissViewControllerAnimated:completion:讓自己消失。
在ViewController中:
AViewController *av = [[AViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:av];
[self presentViewController:nav animated:YES completion:nil];
在AViewController中執(zhí)行disimiss:
[self dismissViewControllerAnimated:YES completion:nil];
對(duì)于這種很常見的場(chǎng)景,這種用法完全沒(méi)問(wèn)題。但是對(duì)于復(fù)雜一點(diǎn)的場(chǎng)景,這種用法就有點(diǎn)蒼白無(wú)力了,先舉個(gè)稍微復(fù)雜一點(diǎn)點(diǎn)的例子:ViewController present AViewController,AViewController present BViewController,在BViewController執(zhí)行完某事件之后需要返回ViewControlle。這個(gè)時(shí)候需要怎樣做呢?如果在BViewController直接執(zhí)行[self dismissViewControllerAnimated:YES completion:nil];的話,它只會(huì)將BViewController消失。
這里你可能會(huì)想到通過(guò)其他方式拿到AViewController,然后調(diào)用AViewController的[self dismissViewControllerAnimated:YES completion:nil];。但是,場(chǎng)景再?gòu)?fù)雜一點(diǎn),在執(zhí)行完各種present和push之后,到達(dá)了XViewController,在XViewController中執(zhí)行成功任務(wù)之后需要回到ViewController,這個(gè)時(shí)候怎么辦呢?我們知道當(dāng)前如果有被present出來(lái)的控制器的情況下,調(diào)用UINavigationController的popToRootViewControllerAnimated:是不起作用的。那么我們?nèi)绾伟堰@個(gè)流程中所有被present和push的控制器給銷毀呢?笨一點(diǎn)的辦法是回溯整個(gè)流程,判斷哪些控制器需要dismiss,哪些控制器需要pop。但這種方式顯然有點(diǎn)低效和難以控制,下面我們來(lái)看看到底該怎么使用dismissViewControllerAnimated:completion:。
我們先看看官方文檔到底怎么講的:
Dismisses the view controller that was presented modally by the view controller.
The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.
If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.
可以簡(jiǎn)單歸納為兩點(diǎn):
第一點(diǎn):誰(shuí)present出來(lái)的控制器,誰(shuí)負(fù)責(zé)把它dismiss掉,但是如果你在被present出來(lái)的控制器中調(diào)用dismiss的話,UIKit會(huì)自動(dòng)讓它的presenting控制器(找到誰(shuí)把它present出來(lái)的)去執(zhí)行dismiss。
第二點(diǎn):如果你present了一系列的控制器,那么系統(tǒng)會(huì)把被present出來(lái)的控制器放在一個(gè)棧中,當(dāng)處在底層的控制器執(zhí)行dismiss的時(shí)候,在它之后被present出來(lái)的控制器都會(huì)被移除,只有棧頂上的控制器會(huì)有dismiss動(dòng)畫。
另外補(bǔ)充相關(guān)的兩點(diǎn):
第一點(diǎn):當(dāng)被present出來(lái)的控制器的modalPresentationStyle = UIModalPresentationFullScreen時(shí),執(zhí)行當(dāng)前present事件的控制器必須是一個(gè)全屏控制器,如果當(dāng)前執(zhí)行的控制器不是一個(gè)全屏的控制器,它將在視圖層級(jí)結(jié)構(gòu)中找到一個(gè)全屏的父類控制器去執(zhí)行present事件。也就是說(shuō)如果A執(zhí)行present B,那么B.presentingViewController不一定是A。比如你當(dāng)前的控制器A在導(dǎo)航nav中,A present B之后,實(shí)際上B.presentingViewController指向的是nav而不是A。
第二點(diǎn):self.presentingViewController,它指向的是把當(dāng)前控制器present出來(lái)的控制器或者是把當(dāng)前控制器的最上層的父類present出來(lái)的控制器。
通過(guò)上面的文檔介紹,我們可以看到在本文剛開始介紹的最簡(jiǎn)單的使用場(chǎng)景下(ViewController present AViewController),在AViewController中執(zhí)行[self dismissViewControllerAnimated:YES completion:nil]和在ViewController中執(zhí)行[self dismissViewControllerAnimated:YES completion:nil]效果是一樣的,這一點(diǎn)是因?yàn)橄到y(tǒng)幫我們處理好了(因?yàn)橄到y(tǒng)判判AViewController當(dāng)前沒(méi)有present出來(lái)任何控制器,所以系統(tǒng)會(huì)找到它的presentingViewController,也就是ViewController來(lái)執(zhí)行dismiss事件)。在復(fù)雜一點(diǎn)的情況下,比如我們要dismiss掉當(dāng)前被present出來(lái)的控制器的話,我們就需要想辦法拿到處在棧底的那個(gè)控制器,在這個(gè)控制器中執(zhí)行[self dismissViewControllerAnimated:YES completion:nil]才行。
那么很顯然,執(zhí)行[self dismissViewControllerAnimated:YES completion:nil]的流程是這樣子的:

在我們上面講的復(fù)雜場(chǎng)景下,我們?cè)趺匆淮涡园旬?dāng)前present出來(lái)的控制都dismiss掉呢?可以通過(guò)下面的方式來(lái)查找到最頂層的presentingViewController(其實(shí),通常是我們window的rootViewController)讓它來(lái)執(zhí)行dismiss就好了,剩下的工作可能就是處理一下導(dǎo)航中的控制器了。
比如我們?cè)诮?jīng)過(guò)各種present和push之后才到達(dá)的XViewController頁(yè)面中執(zhí)行如下代碼:
UIViewController *presentingVc = self.presentingViewController;
while (presentingVc.presentingViewController) {
presentingVc = vc.presentingViewController;
}
if(presentingVc){
[presentingVc dismissViewControllerAnimated:YES completion:nil];
}