0
votes

[SOLVED]

Let me explain my problem;

I have a bunch of Event object which appears in tableView cells. enter image description here

And these objects store in events array

var events: [Event]

Also i have another view controller(DetailViewContoller), in that view controller i have a variable for receiving event object from tableview.

    import UIKit

class EventDetailViewController: UIViewController {


    var event: Event?

    override func viewDidLoad() {
        super.viewDidLoad()
        print(event?.name)
    }

When i tapped the cells i want to pass my current event object to variable(indicated on above) in DetailViewController. So i tried this ->

extension EventListViewcontroller:UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // Whenever cells tapped, related object needs to pass DetailViewController
    performSegue(withIdentifier: K.Segues.eventListToDetail, sender: self)

   func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if segue.identifier == K.Segues.eventListToDetail {
          let VC = segue.destination as! EventDetailViewController
        VC.event = events[indexPath.row]
      }
  }

}

But unfortunately after segue performed when i checked the event variable in DetailViewController it was shown as a nil. So question is, how can i pass my tapped event cell to other view controller?

Note: i've found this topic How do you test UIStoryBoard segue is triggered by didSelectRow(at:) but it didn't help my problem...

1
is your condition if segue.identifier == K.Segues.eventListToDetail { is true? Is control even going inside if and setting VC.event = events[indexPath.row]?Sandeep Bhandari
Yes it's true because when i tapped the cells it performs segue but it prints event?.name as a "nil" :/Rutkay Karabulak
performing segue doesn't confirm if your condition in prepareForSegue is satisfied, put a break point inside prepareForSegue and check, I am assuming posted code is messed up here and you really have prepareForsegue as different method and not nested inside didSelectSandeep Bhandari
@SandeepBhandari i did put and i checked, and i realize that it doesn't trigger prepare segue at all...Rutkay Karabulak
Thats the bug, fix it you will have your problem solvedSandeep Bhandari

1 Answers

0
votes

In your class

class ViewController: EventListViewcontroller {
     var selectedIndex: IndexPath? = nil
}

And change your code in didSelect as

extension ViewController:UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // Whenever cells tapped, related object needs to pass DetailViewController
        self.selectedIndex = indexPath
        performSegue(withIdentifier: K.Segues.eventListToDetail, sender: self)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let indexPath = self. selectedIndex, segue.identifier == K.Segues.eventListToDetail {
            let VC = segue.destination as! EventDetailViewController
            VC.event = events[indexPath.row]
        }
    }
}

Hope this helps