Access object of performSegue?

Hello,

I'm having a little trouble performing a custom segue with a UIButton in a custom TableViewCell. My app is two tableviews, and when you tap the button in a cell in the parent tableView, the app should segue to display a unique child list based on the indexPath of the cell tapped. I've tried doing a performSegue in my button action, but it won't let me pass the indexPath as the "object" in performSegue. Is there a way to access the object of performSegue in prepare(for: segue)? Any help would be greatly appreciated.

Thanks.

Replies

You should show the code, that would be easier.


There is a simpler way to achieve what you want: don't pass the objects inside the perform segue, but set directly in prepare:


In the desination controller (DestinationController class for instance), declare an indexPath

var receivedIndexPath: IndexPath?


In the originating class, declare a var to hold the selected index

var selectedIndexPath: IndexPath?


In tableView(didSelect), set the value

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     // do what you need here
          selectedIndexPath = indexPath

}


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

        // Pass the selected object to the new view controller.
        if let destVC = segue.destination as? DestinationController {
            if selectedIndexPath != nil { destVC.receivedIndexPath = selectedIndexPath!}
        }
    }


You can now use receivedIndexPath in the desination controller

Does this new question have something to do with your former question How to get variable in TableViewCell from TableView?


You should better not keep your old thread untouched, especially when it has some replies.

If you have solved your issue yourself (including asking in another site), at least you should report it as solved.


And please show your latest code. The description you wrote about your `object` is hard to understand, showing your code tells us better than words.


And if there is nothing new than the old question, you should better go back to the old thread and consider applying my reply, that would be the fastest way to solve your issue.