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

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

问题描述

我正在尝试编写一个刽子手游戏,并且在解开细分时遇到了麻烦。我有多个视图控制器,最终导致相同的视图,用户在其中播放实际的刽子手。
但是,根据呈现控制器,我希望游戏处于不同的模式(即:多人游戏,单人游戏等)。我正在尝试添加一个再次播放按钮,该按钮可以展开前一个视图控制器,但是当用户可以使用多个路径进入此视图时,我不确定如何展开。

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!

推荐答案

展开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:


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

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


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

  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 数组中搜索想要
的视图控制器处理展开动作。如果接收者的子视图
控制器都没有想要处理展开动作,则接收者检查
是否要处理展开动作并且如果
那样返回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 - 例如,通过模式演示segue或 UINavigationController上的推送segue 但是只要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)当多个视图控制器导致相同视图时展开segue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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