下一个以模态方式出现时,立即关闭或删除上一个以模态呈现的视图控制器 [英] Dismiss or remove previous modally presented view controller as soon as the next one appear modally

查看:18
本文介绍了下一个以模态方式出现时,立即关闭或删除上一个以模态呈现的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标包括很多视图,需要根据每个用户操作以模态方式呈现不同的视图.这是我想要做的,以获得更清晰的视图层次结构和更好的用户体验.

<块引用>

  1. 根视图控制器以模态方式呈现第一个视图控制器
  2. 当我点击第一个视图控制器上的按钮时,第二个视图控制器会模态地出现在它上面.
  3. 第二个视图控制器出现后,我想从视图层次结构中关闭或删除第一个.

我可以这样做吗?如果是这样,我该怎么做?

如果不是,解决这个问题的正确方法是什么,因为我将在每个视图上呈现许多模态呈现的视图控制器.我想即使我想关闭当前视图,当当前视图关闭时,前一个仍然会出现.

更新:

<块引用>

VC1(根)> VC 2(以模态存在)> VC 3(以模态呈现在 VC 2)

当我关闭 VC3 时,VC2 仍在视图内存中.因此,我不想在关闭 VC3 后立即出现 VC2,而是希望通过删除或关闭 来查看 VC1>VC2 来自视图层次结构.

WANT :在图像中,当我关闭蓝色时,我不想在我的视图内存中看到粉红色,我想在蓝色出现时立即将其删除.

这就是我想做的.

有什么帮助吗?谢谢.

解决方案

那么,假设您有一个类似于以下内容的故事板:

应该发生的是:

  • 呈现第二个 ViewController(来自第一个 ViewController).
  • 展示第三个 ViewController(来自第二个 ViewController).
  • 解除第一个 ViewController(从第三个 ViewController).

在第三个 ViewController 按钮的动作中:

@IBAction func tapped(_ sender: Any) {presentingViewController?.presentingViewController?.dismiss(动画:true,完成:nil)}

如您所见,通过访问当前的

如果您要求删除中间的 ViewController(在本例中为第二个 ViewController),dismiss(animated:completion:) 自动执行此操作:

<块引用>

如果连续呈现多个视图控制器,从而构建一个呈现的视图控制器堆栈,在视图上调用此方法堆栈中较低的控制器解除其直接子视图控制器和堆栈中该子级上方的所有视图控制器.发生这种情况时,只有最顶层的视图会在动画中消失时尚;任何中间视图控制器都被简单地从堆.使用其模态转换关闭最顶层的视图样式,可能与其他视图控制器使用的样式不同在堆栈中较低.

指的是你在问什么:

<块引用>

我认为即使我想关闭当前视图,前一个视图也会当前一个关闭时仍然出现.

我认为这在 UI 上显示得很清楚(我觉得还可以),但是正如 dismiss 文档讨论中提到的,第三个和第二个都将从堆栈中删除.这是正确的方法.

My target include a lot view need to present different view modally base on each user action. Here what I want to do to get cleaner view hierarchy and better user experience.

  1. Root View Controller present First View Controller modally
  2. When I clicked button on the First View Controller, then the Second View Controller appear modally over it.
  3. As soon as the Second View Controller did appear, I want to dismiss or remove the first one from view hierarchy.

Can I do that? If so, how should i do it?

If not, what is the right way to solve this out cause I will present many modally presented view controllers over each view. I think even if I want to dismiss current view, the previous one will still remain appear when current one dismiss.

UPDATE :

VC1 (Root) > VC 2 (which was present modally) > VC 3 (which was present modally over VC 2)

When i dismiss VC3, the VC2 is still on view memory. So, I don't want to appear VC2 as soon as I dismiss VC3 and instead I want to see VC1 by removing or dismissing VC2 from view hierarchy.

WANT : At the image, when I dismiss the blue,I don't want see the pink in my view memory and I want to remove it as soon as the blue one appear.

That's what i want to do.

Any Help?Thanks.

解决方案

So, let's assume that you have a storyboard similar to:

What should happens is:

  • Presenting the the second ViewController (from the first ViewController).
  • Presenting the the third ViewController (from the second ViewController).
  • dismissing to the first ViewController (from the third ViewController).

In the third ViewController button's action:

@IBAction func tapped(_ sender: Any) {
    presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
}

As you can see, by accessing the presentingViewController of the current ViewController, you can dismiss the previous hierarchy of the view controllers:

The view controller that presented this view controller.

By implementing presentingViewController?.presentingViewController? that means that: the presented of the presented current ViewController :)

It might seem a little bit confusing, but it is pretty simple.

So the output should be like (I added background colors to the viewControllers -as vc1: orange, vc2: black and vc3: light orange- to make it appears clearly):

EDIT:

If you are asking to remove the ViewController(s) in the middle (which in this example the second ViewController), dismiss(animated:completion:) does this automatically:

If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

Referring to what are you asking:

I think even if I want to dismiss current view, the previous one will still remain appear when current one dismiss.

I think that appears clearly on the UI (and I find it ok), but as mentioned in the dismiss documentation discussion, both the third and the second will be removed from the stack. That's the right way.

这篇关于下一个以模态方式出现时,立即关闭或删除上一个以模态呈现的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆