一次关闭多个模态视图控制器?
因此,有一个包含三个视图控制器的堆栈,其中 A 是根,B 是第一个模态视图控制器,C 是第三个模态 vc。我想立即从 C 转到 A。我已经尝试过 这个解决方案解雇。它确实可以工作,但方式不正确。也就是说,当最后一个视图控制器被关闭时,它将在第一个视图控制器显示之前短暂显示第二个视图控制器。我正在寻找的是一种从第三个 vc 到一个漂亮动画中的第一个 vc 的方法,而不会注意到第二个视图。对此的任何帮助都非常感激。
So have a stack with three view controllers where A is root, B is first modal view controller and C is third modal vc. I would like to go from C to A at once. I have tried this solution to dismiss.It does work but not in a correct way. That is when the last view controller is dismissed it will breifly show the second view controller before the first is shown. What I'm looking for is a way to get from the third vc to the first in one nice animation without noticing the second view. Any help on this is greatly appriciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
确保您只调用
dismissModalViewControllerAnimated:
一次。我发现要求关闭每个堆叠的模态视图控制器将导致它们都产生动画。
您有:
A =modal>; B=模态> C
你应该只调用
[myViewControllerA解雇ModalViewControllerAnimated:YES]
如果你使用
[myViewControllerB解雇ModalViewControllerAnimated:YES]
,它会解雇C,而不是B。在正常情况下(unstacked)使用,它会解雇B(由于响应者链将消息冒泡到A)。在您描述的堆叠场景中,B 是父视图控制器,这优先于模态视图控制器。Be sure that you're only calling
dismissModalViewControllerAnimated:
once.I have found that asking to dismiss each stacked modal view controller will cause both of them to animate.
You have:
A =modal> B =modal> C
You should only call
[myViewControllerA dismissModalViewControllerAnimated:YES]
If you use
[myViewControllerB dismissModalViewControllerAnimated:YES]
, it will dismiss C, and not B. In normal (unstacked) use, it would dismiss B (due to the responder chain bubbling the message up to A). In the stacked scenario that you describe B is a parent view controller and this takes precedence over being a modal view controller.尽管接受的答案确实对我有用,但它现在可能已经过时了,并且留下了一个看起来很奇怪的动画,其中最上面的模态将立即消失,并且动画将位于后面的模态视图上。我尝试了很多方法来避免这种情况,最终不得不使用一些技巧来让它看起来不错。
注意:(仅在 iOS8+ 中测试,但应该适用于 iOS7+)
基本上,viewControllerA 创建一个以
viewControllerB
作为 rootview 的UINavigationController
并以模态方式呈现它。现在在
viewControllerB
中,我们将以同样的方式呈现viewControllerC
,但是在呈现它之后,我们将把viewControllerC
的快照放在上面viewControllerB
导航控制器上的视图层。然后,当viewControllerC
在解雇期间消失时,我们将看不到变化,并且动画看起来很漂亮。下面是我的辅助函数,用于呈现视图和处理解雇。
需要注意的一件事是,我使用 Purelayout 来添加自动布局约束。您可以修改它以手动添加它们或在以下位置获取 Purelayout
https://github.com/PureLayout/PureLayout
现在我们只需要确保我们将missBlock定义为ViewControllerC.h 中的一个属性(显然,您可以用委托方法或其他同样令人兴奋的设计模式替换整个部分,重要的部分是在 viewControllerB 级别处理解雇)
希望这有帮助,快乐的编程:)
Although the accepted answer did work for me, it may be outdated now and left a weird looking animation where the topmost modal would immediately disappear and the animation would be on the rear modalview. I tried many things to avoid this and ended up having to use a bit of a hack to have it look nice.
Note:(only tested in iOS8+, but should work iOS7+)
Basically, viewControllerA creates a
UINavigationController
withviewControllerB
as the rootview and presents it modally.Now in
viewControllerB
we are going to presentviewControllerC
the same way, but after we present it, we are going to put a snapshot ofviewControllerC
over the view layer onviewControllerB
's navigation controller. Then, whenviewControllerC
disappears during dismissal, we won't see the change and the animation will look beautiful.Below are my helper functions that are used to present the view and handle dismissal.
One thing to note, I am using Purelayout for adding auto layout constraints. You can modify this to add them manually or get Purelayout at
https://github.com/PureLayout/PureLayout
Now we just need to ensure we have the dismissBlock defined as a property in ViewControllerC.h (you can obviously replace this whole part with delegate methods or other equally as exciting design patterns, the important part is to handle dismissal at the viewControllerB level)
Hope this helps, happy programming :)
对于任何正在寻找解决方法的人,您可以这样做:
这是代码:
For anyone looking for a work around you can do this:
Here's the code:
您可以通过以下简单方法“回家”:
Here's a simple way in you can "dismiss to home":
您可以在 rootViewController 中关闭这些 modalViewController。
You may dismiss these modalViewControllers at your rootViewController.
您可以递归地找到presentingViewController以到达根:
You can recursively find presentingViewController to get to the root:
你想用的是
popToRootViewControllerAnimated:
。它可以让您到达根控制器,而不显示所有中间控制器。What you want to use is
popToRootViewControllerAnimated:
. It gets you to the root controller without showing all the intervening ones.