(Swift) Expand forking when multiple view controllers lead to the same view?

I'm trying to encode the executioner game, and I have problems with the denouement. I have several view controllers that ultimately lead to the same view in which the user plays the actual executioner. However, depending on the view controller, I want the game to be in different modes (i.e.Multiplayer, single-user, etc.). I'm trying to add a play button again, which turns off to the previous view controller, but I'm not sure how to relax when there are several ways a user could get to get to that view.

In other words, my kind of application goes:

A β†’ B β†’ C or

A β†’ D β†’ C, where C can (ideally) relax to D or B.

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

+11
source share
3 answers

The segue unwinding process will usually determine the previous instance of the UIViewController automatically. The exact process is described in this Tech Note from Apple, but overall:

Starting with the view controller that initiated the unwinding, the search order is as follows:

  • The following view controller in the responder chain sends a viewControllerForUnwindSegueAction:fromViewController:withSender: message. For a view controller, presented modally, it will be which is called presentViewController:animated:completion: Otherwise, parentViewController.

    The default implementation searches for the childViewControllers receiver for the view controller that it wants to process the unwind. If none of the receiver’s child views the controllers want to handle the unwind action, the receiver checks to see if it wants to handle the unwind action and returns itself if it does. In both cases, the canPerformUnwindSegueAction:fromViewController:withSender: used to determine whether the given view controller wants to process the undo action.

  • If the view controller does not return viewControllerForUnwindSegueAction:fromViewController:withSender: in step 1, the search is repeated from the next view controller in the responder chain.

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

+10
source

If you have any user logic and want to programmatically invoke segue unwinding to various view controllers, here's how to do it:

  • Add unwindFromCViewController to both BViewController and DBViewController :

    BViewController.swift

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

    DViewController.swift

     class DViewController : UIViewController { @IBAction func unwindFromCViewController(segue:UIStoryboardSegue) { } } 
  • In your storyboard, create this session and give it an identifier and bind it to the action that you defined above unwindFromCViewController :

enter image description here

enter image description here

  1. Call U-Turn from code:

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

With this, you can relax to the previous view, no matter where it comes from.

+16
source

I would just call popViewController:animated code to go to the view controller if you pushed it on the navigation stack, or dismissViewController:animated if it was presented modally.

+3
source

All Articles