如何将一堆 UIViewControllers 解散到某个 Root ViewController? [英] How to dismiss a stack of UIViewControllers to a certain Root ViewController?
问题描述
某些 RootViewController
呈现一个 ParentViewController
,然后呈现一个 ChildViewController
.
如何在不再次显示 ParentViewController
的情况下将 ChildViewController
动画 直接关闭到 RootViewController
?><小时>
详细
- 假设某些人提供了
ParentViewController
,它允许用户输入一些凭据以登录到某个用户帐户. - 一旦建立连接,
ParentViewController
就会显示ChildViewController
,它向用户显示连接/帐户详细信息 - 当用户关闭
ChildViewController
时,它应该被解除动画(向下滑动等).但不是返回到ParentViewController
用户应该直接返回到RootViewController
当然有可能,ParentViewController
不显示 ChildViewController
本身,而是(以某种方式)告诉 RootViewController
这样做.这样直接从ChildViewController
返回到RootViewController
就没有问题了.然而,这不是我正在寻找的,因为 RootViewController
不应该知道 ChildViewController
甚至不关心 ParentViewController
介绍其他 VC.
我正在寻找一种解决方案,其中 ParentViewController
控制是在它呈现的 VC 被解散后还是显示它的父级(= 根 VC).
代码:
typealias CompletionBlock = () ->空白类 RootViewController: UIViewController {@IBAction func showParentVC(_ sender: Any) {让 parentVC = ParentViewController()parentVC.completion = {self.dismiss(动画:true,完成:nil)}存在(parentVC,动画:真)}}类 ParentViewController: UIViewController {var 完成:CompletionBlock?@IBAction func showChild(_ sender: Any) {让 childVC = ChildViewController()childVC.completion = {self.completion?()}礼物(childVC,动画:真)}}类 ChildViewController: UIViewController {var 完成:CompletionBlock?@IBAction func close(_ sender: Any) {完成?()}}
使用此代码不能解决所描述的问题.如果 close
在 ChildViewController
上被调用,RootViewController
会调用 self.dismiss(animated: true, completion: nil)
.这样 ChildViewController
动画消失并且 ParentViewController
变得可见.然后 ParentViewController
动画消失并且 RootViewController
变得可见.
动画去掉ChildViewController
后,如何跳过ParentViewController
直接显示RootViewController
?
我的建议是将您的 RootViewController 嵌入到 NavigationController 中(如果您还没有),并为父母双方提供一个孩子
navigationController?.present(viewController, animation: true, completion: nil)//而不是 viewController.present(...)
然后你可以在你的 childViewController 中使用这个方法
navigationController?.popToRootViewController(animated: true)
Some RootViewController
presents a ParentViewController
which than presents a ChildViewController
.
How can I dissmiss the ChildViewController
animated directly to the RootViewController
whithout showing the ParentViewController
again?
In Detail
- Assume some presents the
ParentViewController
which lets the user enter some credentials to login to some user account. - Once the connection is established the
ParentViewController
presents theChildViewController
which shows connection / account details to the user - When the user closes the
ChildViewController
it should be dismissed animated (slide down, etc.). But instead of returning to theParentViewController
the user should get back directly to theRootViewController
Of course it would be possible, that the ParentViewController
does not present the ChildViewController
itself but (somehow) tells the RootViewController
to this. This way it would be no problem to directly return from the ChildViewController
to the RootViewController
. However, this is NOT what I am looking for, since the RootViewController
should not know about the ChildViewController
or even care if the ParentViewController
presents other VCs.
I am looking for a solution where the ParentViewController
controls whether itself is shown after the VC it presented is dismissed or its parent (= the root VC).
Code:
typealias CompletionBlock = () -> Void
class RootViewController: UIViewController {
@IBAction func showParentVC(_ sender: Any) {
let parentVC = ParentViewController()
parentVC.completion = {
self.dismiss(animated: true, completion: nil)
}
present(parentVC, animated: true)
}
}
class ParentViewController: UIViewController {
var completion: CompletionBlock?
@IBAction func showChild(_ sender: Any) {
let childVC = ChildViewController()
childVC.completion = {
self.completion?()
}
present(childVC, animated: true)
}
}
class ChildViewController: UIViewController {
var completion: CompletionBlock?
@IBAction func close(_ sender: Any) {
completion?()
}
}
Using this code does NOT solve the described problem. If close
is called on the ChildViewController
the RootViewController
calls self.dismiss(animated: true, completion: nil)
. This way the ChildViewController
animates away and the ParentViewController
becomes visible. Then the ParentViewController
animates away and the RootViewController
becomes visible.
How to skip the ParentViewController
and directly show the RootViewController
after animating away the ChildViewController
?
My recommendation is embed your RootViewController into a NavigationController (if you don't have it yet) and present both parent a child with
navigationController?.present(viewController, animated: true, completion: nil)
//instead of viewController.present(...)
And then youcan use this method from your childViewController
navigationController?.popToRootViewController(animated: true)
这篇关于如何将一堆 UIViewControllers 解散到某个 Root ViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!