iOS開發(fā)_返回頁面時(shí)使用dismiss還是pop?

【作者前言】:13年入圈,分享些本人工作中遇到的點(diǎn)點(diǎn)滴滴那些事兒,17年剛開始寫博客,高手勿噴!以分享交流為主,歡迎各路豪杰點(diǎn)評改進(jìn)!

1.應(yīng)用場景:

操作圖層之間的跳轉(zhuǎn)邏輯時(shí),時(shí)常需要我們區(qū)分頁面在返回時(shí)使用的方法,pop、 dismiss之間做選擇

2.實(shí)現(xiàn)目標(biāo):

在返回的方法中,自動(dòng)處理。如果能夠dismiss就用dismiss,反之用pop。

3.代碼說明:

方法一:通過ViewController的屬性presentingViewController判斷當(dāng)前頁面是否是被present出的,來確定采用dismiss方法
image.png
- (void)backAction
{
    if (self.presentingViewController) 
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    } 
    else 
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
方法二:通過NavgationController的屬性topViewController判斷當(dāng)前頁面是否是被push出的最上層頁面,來確定采用pop方法
- (void)backAction
{
    if (self.navigationController.topViewController == self) 
   {
        [self.navigationController popViewControllerAnimated:YES];
    } 
    else 
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

方法三:通過NavgationController的屬性viewcontrollers數(shù)組索引,來判斷當(dāng)前頁面是否是被push過,來確定采用dismiss方法
- (void)backAction
{
    if ([self.navigationController.viewControllers.firstObject isEqual:self])
    {//當(dāng)前頁面尚未被Push過
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)backAction
{
    if ([self.navigationController.viewControllers indexOfObject:self] == 0)
    {//當(dāng)前頁面尚未被Push過
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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