0

I am trying to get simple Int64 passed between 2 components in Swift, however, the value is never set.

This is my first tableViewController where I want to pass the data 2 from:

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?){
        if (segue.identifier == "transactionDetailSegue") {
            let transactionDetailViewController = segue.destination as? TransactionDetailTableViewController
            transactionDetailViewController?.id = 2
        }
    }

And this is where I want to receive it:

import UIKit

class TransactionDetailTableViewController: UITableViewController {

    @IBOutlet weak var nameText: UILabel!

    var id = 0;

    override func viewDidLoad() {
        super.viewDidLoad()
        dump(id)
    }

}

I made sure twice that the segue identifier is the right one, and checked various tutorials, but I am still not sure what goes wrong.

Thank you

Tj3n
  • 9,837
  • 2
  • 24
  • 35
BarniPro
  • 133
  • 1
  • 2
  • 10

1 Answers1

1

The signature of prepareForSegue is wrong therefore it's never going to be called.

In Swift 3+ the correct signature is

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

and force unwrap TransactionDetailTableViewController

let transactionDetailViewController = segue.destination as! TransactionDetailTableViewController

If everything is hooked up correctly the code must not crash. If it does it reveals a design mistake.

vadian
  • 274,689
  • 30
  • 353
  • 361