unwind segue not triggering

前端 未结 2 1130
醉梦人生
醉梦人生 2021-01-19 18:09

I have been learning swift and have made the foundation of most of my app. I have the following storyboard

app storyboard

Everything works fine. For example

相关标签:
2条回答
  • 2021-01-19 18:38

    Like Craig in the comments said, it's not that easy to find the problem. So I just build a simple app where you can follow the steps as guide and see if you forgot something to setup the functionality right. Hope it will help you. Note: Code is in Swift 3.0, but should be easy to adopt to 2.*

    1. Storyboard with two View Controllers:

    2. Declare the action method for the unwind segue in the FirstViewController.swift:

    class FirstViewController: UIViewController {
      // action method for the unwind segue
      @IBAction func updateScore(_ segue: UIStoryboardSegue) {
        print("Back in the FirstViewController")
      } 
    }
    

    3. Connect the Save button in the Storyboard with the action method (with ctrl + drag):

    4. Connect your Save button with the SecondViewController.swift file, to use it for checking in your prepareSegue method (with ctrl + drag):

    5. Add the prepare(for:sender:) method to your SecondViewController.swift:

    class SecondViewController: UIViewController {
    
      @IBOutlet weak var saveButtonPressed: UIBarButtonItem!
    
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // check safely with guard that your save button is the sender and you can use it
        // if not print message
        guard let uiBarButtonItem = sender as? UIBarButtonItem else {
          print("There is no UIBarButtonItem sender")
          return
        }
    
        // check if you selected the save button
        if saveButtonPressed == uiBarButtonItem {
          print("save button selected")
        }
      }
    }
    

    Result:

    The sample app you can find here

    0 讨论(0)
  • 2021-01-19 18:40

    I did not manage to get the unwind segue to work but instead used

    navigationController!.popViewControllerAnimated(true)
    

    as a work around and this works fine.

    0 讨论(0)
提交回复
热议问题