0

I am trying to pass on information via unwind segue. I have a custom table view and then I click it, it unwinds the segue to the previous segue.

  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let indexPath = self.selectedIndex
        self.performSegue(withIdentifier: "SwapBack", sender: indexPath)
  }

But I also want to be able to pass data to the previous view controller when I unwind the segue:

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

            if let indexPath = sender as? IndexPath {
                let vc = segue.destination as! PopUpViewController
                let post = self.posts[indexPath.row] as! [String: AnyObject]
                let Booked = post["title"] as? String
                let Authors = post["Author"] as? String
                let Prices = post["Price"] as? String
                let imageNames = post["image"] as? String
                self.Booked = Booked
                self.Authors = Authors
                self.imageNames = imageNames
                self.Prices = Prices
                vc.SelectABook.isHidden = true
                vc.SelectBookButton.backgroundColor = UIColor.clear

                print("Is this working?")
            }
 }

In the destination segue. This is the original function that initiates the unwind segue

 @IBAction func Swap (_ sender: UIStoryboardSegue) {

        if sender.source is SwapViewController{
            if let senderVC = sender.source as? SwapViewController{

                self.SwapTitle.text = senderVC.Booked
                self.SwapTax.text = "$0.50"
                self.SwapPrice.text = senderVC.Prices

                if let stringImage = senderVC.imageNames {

                    let imageRef = storage.reference(forURL: "gs://gsignme-14416.appspot.com/images/\(stringImage)")

                    imageRef.data(withMaxSize: 25 * 1024 * 1024, completion: { (data, error) -> Void in
                        if error == nil {
                            self.SwapBook.image = UIImage(data: data!)

                        }else {
                            print("Error downloading image:" )
                        }
                    })
                }
            }
        }
    }

However, when I unwind segue no data is being passed. I put a print statement in the override func but the print statement doesn't print and so I don't know why override func isn't being called, I don't know what I am doing wrong.

Here is a screenshot enter image description here

juelizabeth
  • 485
  • 1
  • 8
  • 31
  • Move most of the code in your `prepare(for:)` function into the `didSelect` function except for the updates to `vc` they belong in the `Swap` function. Also, I suggest you adopt Swift conversions; functions and properties should start with a lower case letter. – Paulw11 Aug 27 '17 at 07:33
  • Have you set breakpoints and stepped through to see what is happening? – Paulw11 Aug 27 '17 at 07:40
  • @Paulw11 when I move it I have a problem with this line `let post = self.posts[(indexPath?.row)!] as! [String: AnyObject]` – juelizabeth Aug 27 '17 at 07:41
  • I get unexpectedly found nil while unwrapping optional value @Paulw11 – juelizabeth Aug 27 '17 at 07:41
  • You are force unwrapping; rarely a good idea as you can get a crash. Use conditional unwrapping and the debugger to examine your variables to see what you are dealing with – Paulw11 Aug 27 '17 at 07:43
  • You can refer to this link https://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them – Rahul Kumar Aug 27 '17 at 10:17

0 Answers0