简体   繁体   English

如何将数据从 TableView 单元格传递到 Swift 5 中的另一个 viercontroller?

[英]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.

相关问题 如何将数据从tableview传递到另一个tableview单元? - how to pass data from tableview to another tableview cell? 将数据快速传递给tableView单元格 - Pass data to tableView cell swift 当在单元格中按下按钮时,如何将数据从一个 Tableview 传递到另一个 Tableview? - how to pass data from one Tableview to another Tableview when button is pressed in cell? 正在将数据从UITableView单元传送到另一个VC的tableview? (迅速) - Carrying on data from a UITableView cell to another VC's tableview? (Swift) 斯威夫特:如何传递子视图控制器prepareForSegue的数据以更改父视图控制器表视图单元格的TextLabel? - Swift: How can I pass data from child viewcontroller prepareForSegue to change the TextLabel of a cell of parent viewcontroller tableview? 如何将数据从tableview传递到像instagram一样的tableview? 迅速 - how to pass data from tableview to tableview like instagram? swift 如何从多个选定的行中获取数据并在另一个VC tableview单元格Swift4中显示 - How to get data from the multiple selected rows and show in another VC tableview cell Swift4 如何将数据从视图控制器传递到另一个表视图? - How to pass data from a viewcontroller to another tableview? Swift:当用户点击Tableview单元格时,如何将数据从一个视图控制器转移到另一个 - Swift: How to SHIFT data from one view controller to another when the user taps on the Tableview Cell 如何将单元格中单击的按钮上的数据传递到另一个表格视图? - How to pass data on button clicked in cell to another tableview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM