Axel Mårtensson
Axel Mårtensson

Reputation: 13

Passing integer with Segue

I have a problem when trying to send data to a new viewController. This line executes

destination1.path = path

Because I get the path to the new ViewController, seems be some problem with integers.

I get this error message:

2017-05-26 20:37:14.442509+0200 Subscription[1224:301622] UIView: 0x105d294f0; frame = (0 0; 414 736); autoresize = W+H; layer = CALayer: 0x17403f360>>'s window is not equal to Subscription.ViewController2: 0x105d3fcb0>'s view's window!

// method to run when table view cell is tapped
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("You tapped cell number \(indexPath.row).")

    let index = indexPath.row
    performSegue(withIdentifier: "EditSegue1", sender: index)


}
@IBAction func backButton(_ sender: Any) {
     self.dismiss(animated: true, completion: nil)
}

// Prepare for next view.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let destination1 = segue.destination as? ViewController2 {

        destination1.path = path

        if let index = sender as? Int {
            destination1.index = index
        }
    }
}

My second viewController.

var path = ""
var index = 0


override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    print(path)
    print(index)
}

Upvotes: 0

Views: 513

Answers (1)

Jake T.
Jake T.

Reputation: 4378

A few issues here: Sender should be an object triggering an event. In most cases, but not all, it should be self.

The second is that you don't have a check for your segue identifier. Everything inside of your prepareForSegue call should be wrapped inside an if( segue.identifier == )` block.

Third, path doesn't appear to be declared in the function you showed, so unless it appears elsewhere, that may cause unintended behavior.

Fourth, since you're trying to use the sender as an int, it's throwing things off. I assume you have a property for path declared. You should probably also declare one for index, and pass that instead of trying to use sender. You also then don't have to wrap it in the type check.

You may also see an issue since you don't declare the types of path and index on your second view controller. You may need to, in order for the first view controller to know what types are allowed to be assigned.

Upvotes: 1

Related Questions