下一个模态出现后,立即解雇或删除以前模态显示的视图控制器 [英] Dismiss or remove previous modally presented view controller as soon as the next one appear modally
问题描述
我的目标包括很多视图,需要根据每个用户操作以模态呈现不同的视图.这是我想要做的事情,以获得更清晰的视图层次结构和更好的用户体验.
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.
- 根视图控制器以模态形式存在第一个View Controller
- 当我单击第一视图控制器"上的按钮时,第二视图控制器将以模态形式显示在其上方.
- 第二个View Controller出现后,我想从视图层次结构中删除或删除第一个.
- Root View Controller present First View Controller modally
- When I clicked button on the First View Controller, then the Second View Controller appear modally over it.
- 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.
更新:
VC1(根目录)> VC 2(以模态形式出现)> VC 3(以前是 通过VC 2模态呈现)
当我关闭VC3
时,VC2
仍在视图内存中.因此,我不想在退出VC3
后立即显示VC2
,而是希望通过从视图层次结构中删除或取消VC2
来看到VC1
.
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 :在图像上,当我关闭蓝色时,我不想在视图内存中看到粉红色,我想在蓝色出现后立即将其删除
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.
这就是我想要做的.
有帮助吗?
推荐答案
所以,我们假设您有一个类似于以下内容的故事板:
So, let's assume that you have a storyboard similar to:
应该发生的是:
- 呈现第二个ViewController(来自第一个ViewController).
- 呈现第三个ViewController(来自第二个ViewController).
- 从第三个ViewController释放到第一个ViewController.
在第三个ViewController按钮的操作中:
In the third ViewController button's action:
@IBAction func tapped(_ sender: Any) {
presentingViewController?.presentingViewController?.dismiss(animated: true, completion: nil)
}
如您所见,通过访问当前版本的 presentingViewController ViewController,您可以关闭视图控制器的先前层次结构:
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.
实现presentingViewController?.presentingViewController?
意味着:当前ViewController的呈现:)
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.
所以输出应该是这样的(我在viewControllers中添加了背景色-如vc1:橙色,vc2:黑色和vc3:浅橙色-以使其清晰可见):
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):
如果您要删除中间的ViewController(在此示例中为第二个ViewController),请
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.
参考您的要求:
我认为,即使我想关闭当前视图,上一个也会 当前的一个解雇仍然存在.
I think even if I want to dismiss current view, the previous one will still remain appear when current one dismiss.
我认为这在UI上很明显(并且我觉得还可以),但是正如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屋!