iOS present返回根視圖解決方案

查了網(wǎng)上很多例子都不能解決我的問題,網(wǎng)上一堆辣雞,然后查了一下資料和譚哥哥一起解決了問題。

去百度云盤看效果:

鏈接: https://pan.baidu.com/s/1Suyloq5g_2RL7HXNtvI0og 提取碼: r3k9 復(fù)制這段內(nèi)容后打開百度網(wǎng)盤手機(jī)App,操作更方便哦

問題:

A present B 之后,B 再 present ?C或者D 的時候,B就會被dismiss掉。

解決方案代碼:

1、加入這個變量記錄當(dāng)前B視圖

```

代碼塊

@property (nonatomic, strong) UIViewController *presentingVC;

```



2、在B中加入重寫:

-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void(^)(void))completion

{

? ? if (self.presentedViewController)

? ? {

? ? ? ? if (self.presentingViewController) {

? ? ? ? ? ? self.presentingVC = self.presentingViewController;

? ? ? ? }

? ? ? ? [super dismissViewControllerAnimated:flag completion:completion];

? ? }

3、從B回到A代碼:

if (self.presentingVC) {

? ? ? ? ? [self.presentingVC dismissViewControllerAnimated:YES completion:nil];

? ? }else{

?? ? ? ? ? [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

? ? }

重點(diǎn)->知識點(diǎn)講解

?self.presentedViewController 是 self.present 出來的 modalVC

?self.presentingViewController 是 super.present 出來的 modalVC

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容