简体   繁体   English

两个模态呈现的视图控制器-都关闭

[英]Two modally presented view controllers - dismiss both

In my app are three view controllers; 在我的应用程序中,有三个视图控制器。 White, red, and blue. 白色,红色和蓝色。 White's is the main view of the app. White是应用程序的主视图。

On button taps, White modally presents Red. 在按钮轻击时,白色将呈现红色。 Red modally presents blue. 红色表示蓝色。 All is well. 一切都很好。 Blue has eg some form for the user to complete, then the user taps Dismiss on Blue. 蓝色具有例如供用户填写的某种形式,然后用户点击蓝色上的关闭。

三视图控制器

Blue would like to return the user to the main UI, white. 蓝色想让用户返回主界面,白色。 When the user taps Dismiss, this runs: 当用户点击“关闭”时,将运行:

@IBAction func didTapDismissBoth(sender: AnyObject) {
    let red = presentingViewController!
    let white = red.presentingViewController!
    white.dismissViewControllerAnimated(true, completion: nil)
}

Both Red & Blue do indeed dismiss, but Blue vanishes immediately and Red is seen doing the dismissal animation! 红色和蓝色确实都可以消除,但是蓝色立即消失 ,可以看到红色在进行消除动画! The flash of red is visually jarring and easily visible in the Simulator when Slow Animations are on. 启用“慢速动画”时,红色闪烁会在视觉上震颤,并且在模拟器中很容易看到。

I would rather see Blue sliding gracefully off screen to reveal White. 我宁愿看到Blue优雅地滑出屏幕,露出White。

Can I prevent Red from rearing its ugly head when returning to White from Blue? 从蓝色返回白色时,我可以防止红色抬起丑陋的头吗?

I was able to simulate the effect I want by (in IB) setting Red's presentation style as "over current context". 通过(在IB中)将Red的表示样式设置为“在当前上下文中”,我能够模拟所需的效果。 This tells UIKit to keep Red's parent's (White) view around, so if eg Red has transparency in its view, White would show through. 这告诉UIKit保持Red的父(White)视图不变,因此,例如,如果Red在其视图中具有透明性,White将显示出来。

In the dismissal process, I set Red to completely transparent, and animate blue away. 在解雇过程中,我将“红色”设置为完全透明,并设置蓝色动画。

@IBAction func didTapDismissBoth(sender: AnyObject) {
    let red = presentingViewController!
    let white = red.presentingViewController!
    red.view.alpha = 0 // Red presents "over current context" so white's view still exists. White's view will show through this transparent red
    red.dismissViewControllerAnimated(true, completion: { // dismiss blue, revealing transparent red & white showing through
        white.dismissViewControllerAnimated(false, completion: nil) }) // dismiss red with no animation
}

Though this hides the problem of Red flashing, it doesn't explain the dismissal animation process; 尽管这掩盖了红色闪烁的问题,但并不能解释解雇动画的过程。 and means White's view stays around unnecessarily. 意味着怀特的观点不必要地徘徊。

I would use unwind segues for this. 我会为此放松一下。 You are using storyboards, why not take advantage of that. 您正在使用情节提要,为什么不利用它。 Just create an unwind segue from the blue view controller to the white one and you are done. 只需创建从蓝色视图控制器到白色视图控制器的放松序列即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 关闭从模态呈现的视图控制器翻转的视图控制器 - Dismiss view controller flipped from modally presented view controller 将以模态形式呈现的视图控制器转移给其他基础视图控制器 - dismiss a modally presented view controller to a different underlying view controller 关闭以模态显示的视图使标签栏控制器(种类)重置 - Dismiss Modally presented view makes tab bar controller (kind of) reset 有没有办法在 Xcode 11 中找到所有模态呈现的视图控制器? - Is there a way to find all modally presented view controllers in Xcode 11? 只要下一个模态显示,就会关闭或删除以前模态显示的视图控制器 - Dismiss or remove previous modally presented view controller as soon as the next one appear modally Swift:展示了两个视图控制器 - Swift: Two View Controllers presented Swift:无法取消模态显示的LoginViewController - Swift: Not able to dismiss modally presented LoginViewController 模态呈现的 TableViewController 抵抗滚动并且难以消除 - TableViewController presented modally resists scrolling and is difficult to dismiss 如何关闭以模态显示的UINavigationController? - How can I dismiss a UINavigationController presented modally? 更改选项卡时以模态显示的ViewController消失 - Dismiss ViewController presented modally when tab is changed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM