iOS开发_返回页面时使用dismiss还是pop?

【作者前言】:13年入圈,分享些本人工作中遇到的点点滴滴那些事儿,17年刚开始写博客,高手勿喷!以分享交流为主,欢迎各路豪杰点评改进!

1.应用场景:

操作图层之间的跳转逻辑时,时常需要我们区分页面在返回时使用的方法,pop、 dismiss之间做选择

2.实现目标:

在返回的方法中,自动处理。如果能够dismiss就用dismiss,反之用pop。

3.代码说明:

方法一:通过ViewController的属性presentingViewController判断当前页面是否是被present出的,来确定采用dismiss方法
image.png
- (void)backAction
{
    if (self.presentingViewController) 
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    } 
    else 
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
方法二:通过NavgationController的属性topViewController判断当前页面是否是被push出的最上层页面,来确定采用pop方法
- (void)backAction
{
    if (self.navigationController.topViewController == self) 
   {
        [self.navigationController popViewControllerAnimated:YES];
    } 
    else 
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

方法三:通过NavgationController的属性viewcontrollers数组索引,来判断当前页面是否是被push过,来确定采用dismiss方法
- (void)backAction
{
    if ([self.navigationController.viewControllers.firstObject isEqual:self])
    {//当前页面尚未被Push过
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)backAction
{
    if ([self.navigationController.viewControllers indexOfObject:self] == 0)
    {//当前页面尚未被Push过
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容