-1

My scenario, I am trying to get JSON data using codable format. I need to pass the decoder value to another view controller after click the Tableview custom cell. I don't know how to do that, I seen some example but its not clear.

My Code below

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        //print("You tapped cell number \(indexPath.row).")
        let section = isFiltering ? filteredSections[indexPath.section] : sections[indexPath.section]
        let item = section.result[indexPath.row]
        print("\(item)")

        let vc = self.storyboard?.instantiateViewController(withIdentifier: "secondviewcontroller") as! SecondViewController
        vc.dataset = item
        let navigationController = UINavigationController(rootViewController: vc)
        self.present(navigationController, animated: true, completion: nil)
    }
Soniya
  • 1
  • 5
  • Is this code not working? – mmr118 Aug 18 '19 at 15:41
  • I don't know how to retrieve it in another view controller @mmr118 – Soniya Aug 18 '19 at 15:48
  • `SecondViewController.dataset` should hold the `item`. One issue you might have is if the storyboard is performing the segue and you are performing the segue in your code. This would cause 2 controllers to be created, but only the one created from code will hold the data. – mmr118 Aug 18 '19 at 15:59
  • @mmr118 I am going to use present model. Could you please update your answer here. – Soniya Aug 18 '19 at 16:00
  • So to clarify you will use a present model from the storyboard, so only in interfacebuilder and not in the code, correct? – mmr118 Aug 18 '19 at 16:03
  • Storyboard I am using but for present transition I am using code, Which is I mentioned above. @mmr118 – Soniya Aug 18 '19 at 16:10

1 Answers1

-1

Cell to ViewController Segue

This is a segue from a cell to a ViewController that performs on cell selection, for this you want to do:

var selectedItem: Item?

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let secondViewController = (segue.destination as? UINavigationController)?.topViewController as? SecondViewController {
        if let item = selectedItem {
            secondViewController.dataset = selectedItem
        }
    }

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // Here you are only setting the item variable to the selected one so you can grab it in the prepare func
    // No need to call the segue because the storyboard linkage from cell to controller will be used to call the segue
    let section = isFiltering ? filteredSections[indexPath.section] : sections[indexPath.section]
    selectedItem = section.result[indexPath.row]
}

ViewController to ViewController Segue

This is a segue from a ViewController to a ViewController, for this you want to:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let secondViewController = (segue.destination as? UINavigationController)?.topViewController as? SecondViewController {
        if let item = sender as? Item {
            secondViewController.dataset = selectedItem
        }
    }

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    // Here the controller will not automatically perform the segue on selection
    // You call the performSegue function and send the selected item as sender
    // The item will be available in the prepare function
    let section = isFiltering ? filteredSections[indexPath.section] : sections[indexPath.section]
    performSegue(withIdentifier: "secondviewcontroller", sender: section.result[indexPath.row])
}
mmr118
  • 496
  • 4
  • 14
  • this is not I am expecting. I am asking code transition to pass the value and get another viewcontroller. Please check my UITableView, didSelectRowAt inside vc.item. I need to pass it and get it in another view controller. – Soniya Aug 18 '19 at 16:30
  • You are passing it, but maybe the way other points are set up is making it so you are not on the right viewController. I am not sure. Your first code should work. What I sent were alternatives and WILL do what you want to do. – mmr118 Aug 18 '19 at 16:39
  • Its working by **print("OUTPUT: \(dataset[0].id)")** – Soniya Aug 18 '19 at 17:04