UIios 常用知识点详解iOS

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

2019-02-27  本文已影响655人  iOS_PM_WEB_尛鹏

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

猜你喜欢

热点阅读