显示连续模态视图的正确方法
我有两个视图需要以模态方式显示,一个接一个。如果我们连续关闭并显示,这将不起作用,如下所示:
[rootController dismissModalViewControllerAnimated: YES];
[rootController presentModalViewController: psvc animated: YES];
第二个模态视图根本不显示。
我见过这样的修复:
[rootController dismissModalViewControllerAnimated: YES];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[self performSelector: @selector(seekModal) withObject: nil afterDelay: 0.5];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
问题是这不会一直有效(有时所需的延迟是优越的)。
另一个可能的修复方法是消除动画:
[rootController dismissModalViewControllerAnimated: NO];
[rootController presentModalViewController: psvc animated: YES];
但我真的很想保留动画,以保持第一个模式不妨碍的感觉。有什么建议吗?
I have two views that need to be shown modally, one after the other. This doesn't work if we dismiss and show consecutively, like this:
[rootController dismissModalViewControllerAnimated: YES];
[rootController presentModalViewController: psvc animated: YES];
The second modal view simply doesn't show up.
I've seen a fix that was something like this:
[rootController dismissModalViewControllerAnimated: YES];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[self performSelector: @selector(seekModal) withObject: nil afterDelay: 0.5];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
The problem is that this won't work all the time (the delay needed is superior, sometimes).
Another possible fix would be to eliminate the animation:
[rootController dismissModalViewControllerAnimated: NO];
[rootController presentModalViewController: psvc animated: YES];
But I'd really like to keep the animation, to keep the feel that the first modal is out of the way. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
编辑:在iOS5+中执行此操作的“正确”机制是使用
–dismissViewControllerAnimated:completion:
方法,并从完成块中呈现顺序视图控制器。一旦模态解除动画完成,以模态方式显示的视图控制器将调用其 viewDidDisappear:animated: 方法。 AFIK 这是您可以挂钩以启动后续的presentModalViewController:animated: 调用的唯一地方。
我有一个用于呈现模式视图控制器的类,一旦解除完成,它就会通过回调呈现视图控制器来实现您正在寻找的逻辑。要使用此类,只需分配/初始化一个实例并使用正常的 presentViewController:animated: 调用来呈现。在呈现视图控制器上实现以下方法:
一旦模态视图控制器消失,就会调用该方法,此时您可以呈现一个新的模态视图控制器。
还有一件好事 - 由于此类是 UINavigationController 的特化,因此您可以根据需要配置导航栏开/关。该类还具有内置逻辑,可以根据需要显示关闭按钮。
这是类定义:
以及类实现:
并且,这是如何使用它(在某些普通视图控制器的上下文中):
并且,这是解雇回调方法,它呈现了一个新的模式视图控制器:
EDIT: The "correct" mechanism to do this in iOS5+ would be to use the
– dismissViewControllerAnimated:completion:
method, and present the sequential view controller from the completion block.The viewcontroller that is being shown modally will have its viewDidDisappear:animated: method called once the modal-dismissal-animation is complete. AFIK this is the only place you can hook to initiate a subsequent presentModalViewController:animated: call.
I have a class that I use for presenting modal view controllers and it implements the logic you're looking for via a callback to the presenting view controller once the dismissal is complete. To use this class, simply alloc/init an instance and present using the normal presentViewController:animated: call. Implement the following method on the presenting view controller:
This will be called at once the modal view controller is gone, and you can present a new modal view controller at this time.
One nice thing too - since this class is a specialization of UINavigationController, you can configure the navigationBar on/off as you like. The class also has built-in logic to show a dismiss button, as you like.
Here's the class definition:
And the class implementation:
and, here's how you can use it (in the context of some normal view controller):
and, here is the dismissal callback method, which presents a new modal view controller:
rootController 可以判断其顶部的最后一个模态视图控制器何时消失,因为它将收到 viewDidAppear:。您是否尝试过将后续视图控制器的 presentModalViewController: 链接到该控制器?
rootController can tell when the last of the modal view controllers on top of it has disappeared because it will receive a viewDidAppear:. Have you tried linking the presentModalViewController: of the subsequent view controller to that?
如果您确实想将多个视图动画链接在一起,我实际上建议您自己处理动画逻辑。这并不太棘手,然后您可以对视图的呈现方式进行细粒度的控制。我刚刚在这里为另一个问题写了类似的内容:
iOS -- 如何控制模态视图控制器的大小?
你可以只对视图进行动画显示,对视图进行动画关闭,当你的animationDidStop选择器被调用时,对你的第二个视图进行动画处理视图上。这样做的好处是您还可以使用视图不透明度和动画方向,以及准确决定视图何时应出现。例如,当第一个视图滑开时,您可以让第二个视图在第一个视图上向上滑动;无需等待第一个动画完成。
If you really want to chain multiple view animations together, I would actually suggest just handling the animation logic yourself. It's not too tricky, and then you can have fine-grained control over how the views are presented. I just wrote up something similar for another question here:
iOS -- how do you control the size of a modal view controller?
You can just animate the view on, animate the view off, and when your animationDidStop selector is called, animated your second view on. The nice part of this is you can also play with view opacity and animation direction, as well as decide exactly when the views should appear. For example, you could have the second view come sliding up over the first view as the first view is sliding away; no need to wait for the first one to complete its animation.
您的问题与“在模态视图内显示模态视图”相关吗?
我在这里发布了有关此问题的答案:
iPhone 模态视图位于另一个模态视图内?
Is your issue related to "show a modal view inside a modal view" ?
I've post an answer about this here :
iPhone modal view inside another modal view?
我发现对于这样的事情(如果它们都是父视图的相同子级)的最佳解决方案是将它们的视图修补到启用分页的 UIScrollView 上,(您可以在底部添加一个页面控件以使其清晰并用于导航)然后在控制器的视图出现在屏幕上时将它们添加到页面视图中,在它们离开屏幕时将其删除。
如果控制器依赖于此调用,您可能还必须虚拟调用 -viewWillAppear 和 -viewWillDisappear。当你对所有内容进行编码时,感觉有点像黑客,但是一旦你让它工作起来,它看起来就很流畅和自然,并且没有任何与将一个视图动画化出来然后将下一个视图动画化相关的等待。消失了。
The best solution I found for something like this (if they are all equal children of the parent view) is to patch their views onto a UIScrollView with paging enabled, (you can add a page control at the bottom to make it clear and for navigation) then add the views of the controllers to the page view as they come onscreen, remove as they go offscreen.
You may also have to dummy call -viewWillAppear and -viewWillDisappear if the controllers rely on this being called. It feels kinda hack-ish when you're coding it all, but once you have it working it looks smooth and natural, and there isn't any of the waiting associated with animating one view out, then animating the next one in once it is gone.
我发现使用模态视图的 -viewDidDissapear 来调用呈现视图控制器上的方法效果非常好。好处之一是能够延迟模态视图控制器上的释放。请发布我可以做出的任何改进。
我创建这个协议的灵感来自于 iOS 5 对 UIViewController 的“dismissViewControllerAnimated:completion:”补充。我希望在 iOS 4.3 中提供此功能。
PresentorDelegateProtocol.h
PresentingViewController.h
ModalViewController.h
PresentingViewController.m
ModalViewController.m
I find using the modal view's -viewDidDissapear to invoke methods on the presenting view controller work's very well. One benefit is the ability to delay deallocation on the modal view controller. Please post any improvements I can make.
My inspiration for creating this protocol came from iOS 5's "dismissViewControllerAnimated:completion:" addition to UIViewController. I wanted this functionality in iOS 4.3.
PresentorDelegateProtocol.h
PresentingViewController.h
ModalViewController.h
PresentingViewController.m
ModalViewController.m