(Swift) 当多个视图控制器导致同一个视图时,展开转场? [英] (Swift) Unwind segue when multiple view controllers lead to same view?

查看:17
本文介绍了(Swift) 当多个视图控制器导致同一个视图时,展开转场?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个刽子手游戏,但在解除转场时遇到了问题.我有多个视图控制器,它们最终都指向同一个视图,用户在其中播放实际的刽子手.但是,根据呈现的控制器,我希望游戏处于不同的模式"(即:多人游戏、单人游戏等).我正在尝试添加一个重新播放按钮,该按钮可展开到前一个视图控制器,但我不确定当用户可以通过多条路径到达此视图时如何展开.

I am trying to code a hangman game and am having trouble with unwind segues. I have multiple view controllers that all ultimately lead to the same view, where the user plays the actual hangman. However, depending on the presenting controller, I want the game to be in different "modes" (ie: multiplayer, single player, etc.). I am trying to add a play again button that unwinds to the previous view controller, but am unsure how to unwind when there are multiple paths the user could have taken to get to this view.

换句话说,我的应用程序是这样的:

In other words, my app kind of goes:

A -> B -> C 或

A -> B -> C or

A -> D -> C,其中 C 可以(理想情况下)展开到 D 或 B.

A -> D -> C where C can (ideally) unwind to D or B.

我想知道实现这一点的最佳方法是什么?我应该将所有视图控制器嵌入到导航控制器中吗?或者有没有办法根据特定条件呈现某个视图控制器?感谢您的帮助!

I was wondering what the best way to implement this is? Should I just embed all my view controllers in navigation controllers? Or is there a way to present a certain view controller based on a certain condition? Thank you for any help!

推荐答案

unwind segue 过程通常会自动确定之前的 UIViewController 实例.Apple 的这个技术说明中描述了确切的过程,但是总结:

The unwind segue process will generally determine the previous UIViewController instance automatically. The exact process is described in this Tech Note from Apple, but in summary:

从启动 unwind segue 的视图控制器开始搜索顺序如下:

Starting from the view controller that initiated the unwind segue the search order is as follows:

  1. 响应者链中的下一个视图控制器被发送一个viewControllerForUnwindSegueAction:fromViewController:withSender:信息.对于模态呈现的视图控制器,这将是调用的视图控制器presentViewController:animated:completion:.否则,父视图控制器.

  1. The next view controller in the responder chain is sent a viewControllerForUnwindSegueAction:fromViewController:withSender: message. For a view controller presented modally, this will be the view controller that called presentViewController:animated:completion:. Otherwise, the parentViewController.

默认实现搜索接收者的 childViewControllers 数组,用于需要的视图控制器处理展开动作.如果没有接收者的子视图控制器想要处理展开动作,接收器检查是否要处理 unwind 动作并返回 self 如果它做.在这两种情况下,canPerformUnwindSegueAction:fromViewController:withSender: 方法是用于确定给定的视图控制器是否想要处理放松动作.

The default implementation searches the receiver's childViewControllers array for a view controller that wants to handle the unwind action. If none of the receiver's child view controllers want to handle the unwind action, the receiver checks whether it wants to handle the unwind action and returns self if it does. In both cases, the canPerformUnwindSegueAction:fromViewController:withSender: method is used to determine if a given view controller wants to handle the unwind action.

如果没有返回视图控制器viewControllerForUnwindSegueAction:fromViewController:withSender: 在第一步,搜索从下一个视图控制器开始重复响应链.

If no view controller is returned from viewControllerForUnwindSegueAction:fromViewController:withSender: in step one, the search repeats from the next view controller in the responder chain.

因此,精确的过程将取决于您如何呈现视图控制器 C - 例如,通过模态呈现转场或 UINavigationController 上的推送转场,但只要 B 和 D 都实现了放松动作你应该会很好.

So, the precise process will depend on how you presented view controller C - for example, via a modal presentation segue or a push segue on a UINavigationController but as long as both B and D implement the unwind action you should be good.

这篇关于(Swift) 当多个视图控制器导致同一个视图时,展开转场?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆