【作者前言】: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];
}
}