Ios 使用委托协议将数据从tableView单元格传递到另一个视图控制器

Ios 使用委托协议将数据从tableView单元格传递到另一个视图控制器,ios,swift,Ios,Swift,如何使用代理协议swift 4将数据从tableViewCell传递到另一个视图控制器,并在标签中打印数据,只需为要传递到另一个视图控制器中的值取一个变量,然后在按下按钮的同时传递值即可 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ let vc = self.storyboard.instantiateViewController(withIdentifier: "Anoth

如何使用代理协议swift 4将数据从tableViewCell传递到另一个视图控制器,并在标签中打印数据,只需为要传递到另一个
视图控制器中的值取一个变量,然后在按下按钮的同时传递值即可

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
    let vc = self.storyboard.instantiateViewController(withIdentifier: "AnotherVC") as! AnotherVC
    vc.variable = your value
    self.navigationController?.pushViewController(vc, animated: true)
}
为什么需要使用协议?

func tableView(\utableview:UITableView,didSelectRowAt indepath:indepath){

//将视图控制器值传递给另一个VC时

let vc = self.storyboard.instantiateViewController(withIdentifier: "AnotherVC") as! AnotherVC
vc.variableValue = your value
self.navigationController?.pushViewController(vc, animated: true)
 let vc = self.storyboard.instantiateViewController(withIdentifier: "AnotherVC") as! AnotherVC
 let variable = tableView.cellForRow(at: indexPath)as! YourCell
 vc.varibleValue = cell.value
//您需要将单元格值传递给另一个VC

let vc = self.storyboard.instantiateViewController(withIdentifier: "AnotherVC") as! AnotherVC
vc.variableValue = your value
self.navigationController?.pushViewController(vc, animated: true)
 let vc = self.storyboard.instantiateViewController(withIdentifier: "AnotherVC") as! AnotherVC
 let variable = tableView.cellForRow(at: indexPath)as! YourCell
 vc.varibleValue = cell.value
}


不要使用协议,因为您需要将值从子级传递到父级。在这种情况下,您使用协议。如果您将数据从A传递到B,只需传递值即可

这是您的答案->在发布问题之前,您至少应该尝试一下。或者至少解释一下为什么你已经试过了,或者你需要它做什么。解释更多。为什么它没有用?例如,我们有两个视图控制器A,然后B(B视图控制器从A推出。)A->B。我们希望将数据从B发送到A。然后我们使用协议。但是在你的例子中,我们需要从A->B发送数据,所以在这里你可以直接传递值。我需要使用protocls,因为它只是对我的测试,在IOS中是新的。你需要在B到A之间传递数据吗?