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

前端 未结 3 1275
清酒与你
清酒与你 2020-12-28 09:07

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

3条回答
  •  抹茶落季
    2020-12-28 09:15

    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:

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

    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.

      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.

    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.

    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.

提交回复
热议问题