[英]Passing Data Between Controllers in Swift
I want to passing data between TableViewController and ViewController the program does not go into the method My swift code: 我想在TableViewController和ViewController之间传递数据,程序不进入方法My swift代码:
override func unwind(for unwindSegue: UIStoryboardSegue, towardsViewController subsequentVC: UIViewController) {
let destView : ViewController = unwindSegue.destination as! ViewController
destView.min = Int(minTable)
destView.tableText = unitsText
}
I take data: 我获取数据:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let tableCell = moneyArray[indexPath.row]
minTable = tableCell.val
unitsText = tableCell.name
let _ = navigationController?.popViewController(animated: true)
}
Adn my Table Code: 填写我的表格代码:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath) as! TableViewCell
let tableShow = moneyArray[indexPath.row]
cell.nameCurrency?.text = tableShow.name
cell.valueCarrency?.text = "\(tableShow.val)"
return cell
}
If you want to open a detail view controller when the user clicks on a cell in your main table view controller then the proper way to pass data is by using something like the following: 如果要在用户单击主表视图控制器中的单元格时打开详细信息视图控制器,则传递数据的正确方法是使用类似以下内容的方法:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "MyDetailView") {
// pass data to next view
if let viewController: MyDetailsViewController = segue.destinationViewController as? MyDetailsViewController {
viewController.units = mySelectedTableCell.unitsName
}
}
}
You are using popViewController on your didSelectRow, that means that you are returning on your navigation controller and not pushing a unwind segue or any segue, so you cant use prepareForSegue/unwind method. 您在didSelectRow上使用popViewController,这意味着您将返回导航控制器,而不是推动展开序列或任何序列,因此不能使用prepareForSegue / unwind方法。
One correct way of solving this is using delegation. 解决此问题的一种正确方法是使用委托。
You can find more information about that here: Passing data back from view controllers Xcode 您可以在此处找到有关此信息的更多信息: 从视图控制器Xcode传回数据
But if you want to use unwind segue, you will have to write your unwind method on the previous viewController, not your current. 但是,如果要使用unwind segue,则必须在先前的viewController而不是当前的viewController上编写unwind方法。 Also you will need to use the method performSegue with the identifier of your unwind segue.
同样,您将需要使用performSegue方法和您的解散segue的标识符。
You can see more information about unwind segues here: What are Unwind segues for and how do you use them? 您可以在此处查看有关放松警戒的更多信息: 放松警戒的用途是什么,如何使用它们?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.