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

前端 未结 3 1237
清酒与你
清酒与你 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条回答
  •  -上瘾入骨i
    2020-12-28 09:18

    If you have any custom logic and want to invoke the unwind segue programmatically to different view controllers, here is how:

    1. 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) {
          }
      }
      
    2. In your storyboard, create this segue and give it a identifier, and link it to the action you defined above unwindFromCViewController:

            

            

    1. Invoke the unwind segue from code:

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

    With that, you can unwind to a previous view regardless where it is coming from.

提交回复
热议问题