简体   繁体   English

(Swift)当多个视图控制器导致相同视图时,展开segue?

[英](Swift) Unwind segue when multiple view controllers lead to same view?

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 or A - > B - > C或

A -> D -> C where C can (ideally) unwind to D or B. A - > D - > C其中C可以(理想地)展开到D或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! 感谢您的任何帮助!

If you have any custom logic and want to invoke the unwind segue programmatically to different view controllers, here is how: 如果您有任何自定义逻辑并希望以编程方式将unwind segue调用到不同的视图控制器,请按以下步骤操作:

  1. Add unwindFromCViewController to both BViewController and DBViewController : unwindFromCViewController添加到BViewControllerDBViewController

    BViewController.swift BViewController.swift

     class BViewController : UIViewController { @IBAction func unwindFromCViewController(segue:UIStoryboardSegue) { } } 

    DViewController.swift DViewController.swift

     class DViewController : UIViewController { @IBAction func unwindFromCViewController(segue:UIStoryboardSegue) { } } 
  2. In your storyboard, create this segue and give it a identifier, and link it to the action you defined above unwindFromCViewController : 在您的故事板中,创建此segue并为其指定标识符,并将其链接到您在unwindFromCViewController上面定义的操作:

在此输入图像描述

在此输入图像描述

  1. Invoke the unwind segue from code: 从代码中调用unwind segue:

     self.performSegueWithIdentifier("unwindFromCViewControllerSugueId", sender: self) 

With that, you can unwind to a previous view regardless where it is coming from. 有了它,您可以放松到以前的视图,无论它来自哪里。

The unwind segue process will generally determine the previous UIViewController instance automatically. 展开segue进程通常会自动确定先前的UIViewController实例。 The exact process is described in this Tech Note from Apple, but in summary: Apple的技术说明中描述了确切的过程,但总结如下:

Starting from the view controller that initiated the unwind segue the search order is as follows: 从启动展开segue的视图控制器开始,搜索顺序如下:

  1. The next view controller in the responder chain is sent a viewControllerForUnwindSegueAction:fromViewController:withSender: message. 响应器链中的下一个视图控制器发送一个viewControllerForUnwindSegueAction:fromViewController:withSender: message。 For a view controller presented modally, this will be the view controller that called presentViewController:animated:completion: . 对于以模态方式呈现的视图控制器,这将是调用presentViewController:animated:completion:的视图控制器presentViewController:animated:completion: . Otherwise, the parentViewController. 否则,parentViewController。

    The default implementation searches the receiver's childViewControllers array for a view controller that wants to handle the unwind action. 默认实现在接收者的childViewControllers数组中搜索想要处理展开操作的视图控制器。 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. 如果接收者的子视图控制器都不想处理展开动作,则接收器检查是否要处理展开动作并返回self(如果是)。 In both cases, the canPerformUnwindSegueAction:fromViewController:withSender: method is used to determine if a given view controller wants to handle the unwind action. 在这两种情况下, canPerformUnwindSegueAction:fromViewController:withSender:方法用于确定给定视图控制器是否要处理展开操作。

  2. 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. 如果没有从viewControllerForUnwindSegueAction:fromViewController:withSender:返回视图控制器viewControllerForUnwindSegueAction:fromViewController:withSender:在第一步中,搜索从响应器链中的下一个视图控制器重复。

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. 因此,精确的过程将取决于您如何呈现视图控制器C - 例如,通过UINavigationController上的模态演示segue或push segue,但只要B和D都实现了展开动作,您应该是好的。

我只是简单地调用代码popViewController:animated如果你把它推到导航堆栈dismissViewController:animated控制器,或者dismissViewController:animated如果是模态呈现的dismissViewController:animated

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

相关问题 展开前后两个视图控制器之间的Segue - Unwind Segue Between Two View Controllers Back and Forth Swift 取消对子视图控制器行为的预期控制 - Unwind segue to child view controllers not behaving as expected 展开segue不会释放我的视图控制器 - Unwind segue does not release my view controllers 取消搜索-当多个视图调用视图时返回到视图 - Unwind segue - go back to calling view when multiple views call it 如何关闭所有视图控制器,它们在swift中以unwind segue的形式在模板中相互呈现? - how to close all view controllers that presented modally inFront of each other with unwind segue in swift? 在多个视图控制器之间共享数据而无须隔离(Swift 3) - Sharing data between multiple view controllers without segue (Swift 3) 如何使用Swift 3.0以编程方式选择多个视图控制器 - How to segue multiple view controllers programmatically using swift 3.0 带有嵌套模态视图控制器的不合时宜的不希望有的动画 - Undesirable animation with unwind segue with nested modal view controllers 自定义UIStoryboardSegue以展开多个视图控制器 - Custom UIStoryboardSegue to unwind multiple view controllers 放松2个View Controller的搜索 - Unwind segues for 2 View Controllers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM