-1

My question is if it is possible to pass data from a tableview cell that is selected to other view controller

The table view in the first controller uses a prototype cell

First view controller tableview cell prototype

The second view controller has 3 other labels as shown here

Secon viewController preview

I tried what is described here: passing data but it didn't work and gave me the error "Placeholder for UIStoryboardPopoverPresentationSegueTemplate" which I understand from reading this old thread top answer that is because I am using a prototype cell and its dynamic

what I want is that every time an user selects a row, the second view controller is filled with this relation

String 1 → String 4

String 2 → String 5

String 3 → String 6

So far I have tried a couple things like:

     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            tableView.deselectRow(at: indexPath, animated: true)
            let vc = storyboard?.instantiateViewController(identifier:  "secondVC") as! CalculadoraViewController
            present(vc, animated: true)
            let string4 = string4array[indexPath[1]]
            performSegue(withIdentifier: "secVC", sender: string4)            
    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let vc = segue.destination as? CalculadoraViewController {
            vc.string4.text = (sender as! String)
        }
    }

The popover view controller code:

class CalculadoraViewController: UIViewController {

    @IBOutlet weak var string4: UILabel!
    @IBOutlet weak var string5: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49

2 Answers2

1

Here is how you pass data

class CalculadoraViewController: UIViewController {

    @IBOutlet weak var montoUSD: UITextField!
    @IBOutlet weak var montoCRC: UITextField!
    @IBOutlet weak var prVenta: UILabel!
    @IBOutlet weak var prCompra: UILabel!
    var prVentaText:String!
    var prCompraText:String!


    override func viewDidLoad() {

        prVenta.text = prVentaText
        prCompra.text = prCompraText


        montoUSD.placeholder = montoCRC.text
        super.viewDidLoad()
    }
}


   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let vc = storyboard?.instantiateViewController(identifier:  "calculadora") as! CalculadoraViewController

        vc.prCompraText = String(precioCompra[indexPath.row])
        vc.prVentaText =  String(precioVenta[indexPath.row])

    present(vc, animated: true)

}
Jawad Ali
  • 13,556
  • 3
  • 32
  • 49
0

You don't need to perform segue if you are presenting the View-Controller:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = storyboard?.instantiateViewController(identifier: "secondVC") as! CalculadoraViewController
    vc.string4.text = string4array[indexPath.row]
    present(vc, animated: true)         
}
Frankenstein
  • 15,732
  • 4
  • 22
  • 47
  • ok I understand that for me to display the second view controller I don't need the segue, but the question is how to pass data from the selected table view cell to the second view controller – Allanguzmanf May 24 '20 at 06:12
  • It's already given in the answer. `vc.string4.text = "Whatever data you need to pass"` – Frankenstein May 24 '20 at 06:14