[英]How to pass data from TableView cell to another viercontroller in Swift 5?
I am trying to pass data from TableView cell to another ViewController but on the other veiwcontroller passing value found nil.我试图将数据从 TableView 单元格传递到另一个 ViewController 但在另一个 veiwcontroller 传递值发现为零。 Here is the DidSelectRow code:
这是 DidSelectRow 代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = storyboard?.instantiateViewController(identifier: "Test") as? TestVC
present(vc!, animated: true, completion: nil)
vc?.Name = array_product_name[indexPath.row]
}
And here is the another ViewController code:这是另一个 ViewController 代码:
class TestVC: UIViewController {
@IBOutlet weak var nameLbl: UILabel!
var Name : String = ""
override func viewDidLoad() {
super.viewDidLoad()
print("Product Name is:-\(Name)")
nameLbl?.text = Name
}
Your first view controller code is fine.您的第一个视图控制器代码很好。 On the second one instead of setting values in
viewDidLoad
set those in viewWillAppear
.在第二个而不是在
viewDidLoad
中设置值在viewWillAppear
设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.