[英]Passing Data Between Controllers in Swift
我想在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
}
我获取数据:
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
}
如果要在用户单击主表视图控制器中的单元格时打开详细信息视图控制器,则传递数据的正确方法是使用类似以下内容的方法:
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
}
}
}
完整的文档在这里 。
您在didSelectRow上使用popViewController,这意味着您将返回导航控制器,而不是推动展开序列或任何序列,因此不能使用prepareForSegue / unwind方法。
解决此问题的一种正确方法是使用委托。
您可以在此处找到有关此信息的更多信息: 从视图控制器Xcode传回数据
但是,如果要使用unwind segue,则必须在先前的viewController而不是当前的viewController上编写unwind方法。 同样,您将需要使用performSegue方法和您的解散segue的标识符。
您可以在此处查看有关放松警戒的更多信息: 放松警戒的用途是什么,如何使用它们?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.