ios - 在 Swift 中的 Controller 之间传递数据

标签 ios swift uitableview

我想在TableViewController和ViewController之间传递数据 程序不进入方法 我的快速代码:

    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,这意味着您将返回导航 Controller ,而不是推送展开转场或任何转场,因此您无法使用prepareForSegue/unwind 方法。

解决此问题的一个正确方法是使用委托(delegate)。

您可以在这里找到更多相关信息: Passing data back from view controllers Xcode

但是如果你想使用 unwind segue,你必须在前一个 viewController 上编写你的 unwind 方法,而不是当前的 viewController。此外,您还需要使用带有展开转场标识符的 PerformSegue 方法。

您可以在此处查看有关展开转场的更多信息: What are Unwind segues for and how do you use them?

关于ios - 在 Swift 中的 Controller 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41209776/

相关文章:

swift - UITableViewCell 按钮和分段控件

ios - 使用 if 语句将一张图像添加到特定的 UICell 将其添加到多个 UICell Objective-c

iphone - NSNumber 未正确转换并被解释为 NSCFString?

iphone - 如何从 Xcode 项目中删除 iPhone 5 兼容性?

ios - 跨 ViewController 保存信息

arrays - JSON 数据返回带有数组名称的项目名称

ios - 以编程方式添加的约束不起作用

ios - 自调整大小的表格 View 单元格不适用于 swift 中的 ExpandingTableView.framework 库

swift - 按需资源下载后使用文件 Swift 3

iphone - 在 UITableView 中保留占位符单元格