在 Swift 中的控制器之间传递数据 [英] Passing Data Between Controllers in Swift
问题描述
我想在 TableViewController 和 ViewController 之间传递数据程序没有进入方法我的快速代码:
I want to passing data between TableViewController and ViewController the program does not go into the method My swift code:
override func unwind(for unwindSegue: UIStoryboardSegue, towardsViewController subsequentVC: UIViewController) {
let destView : ViewController = unwindSegue.destination as! ViewController
destView.min = Int(minTable)
destView.tableText = unitsText
}
我取数据:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let tableCell = moneyArray[indexPath.row]
minTable = tableCell.val
unitsText = tableCell.name
let _ = navigationController?.popViewController(animated: true)
}
添加我的表格代码:
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
}
推荐答案
您在 didSelectRow 上使用了 popViewController,这意味着您正在返回导航控制器,而不是推送 unwind segue 或任何 segue,因此您不能使用 prepareForSegue/unwind 方法.
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.
解决这个问题的一种正确方法是使用委托.
One correct way of solving this is using delegation.
您可以在此处找到更多相关信息:从视图控制器 Xcode 传回数据
You can find more information about that here: Passing data back from view controllers Xcode
但是如果你想使用 unwind segue,你必须在之前的 viewController 上写你的 unwind 方法,而不是你当前的.此外,您还需要将 performSegue 方法与您的展开转场标识符一起使用.
But if you want to use unwind segue, you will have to write your unwind method on the previous viewController, not your current. Also you will need to use the method performSegue with the identifier of your unwind segue.
您可以在此处查看有关解除转场的更多信息:什么是 Unwind segues 以及如何使用他们?
You can see more information about unwind segues here: What are Unwind segues for and how do you use them?
这篇关于在 Swift 中的控制器之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!