我們都知道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ì)于這種很常見(jiàn)的場(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í)行完某事件之后需要返回ViewController辅搬。這個(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ò)上面的文檔介紹,我們可以看到在本文剛開(kāi)始介紹的最簡(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];
}
轉(zhuǎn)載請(qǐng)注明出處查辩,歡迎關(guān)注支持微信公眾號(hào)“猿故”胖笛!