在Swift

时间:2016-12-18 15:15:44

标签: 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
}

2 个答案:

答案 0 :(得分:0)

如果要在用户单击主表视图控制器中的单元格时打开详细视图控制器,则使用以下内容传递数据的正确方法是:

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
        }
    }
}

完整文档here

答案 1 :(得分:0)

你在didSelectRow上使用popViewController,这意味着你在导航控制器上返回而不是推动展开segue或任何segue,所以你不能使用prepareForSegue / unwind方法。

解决这个问题的一种正确方法是使用委托。

您可以在此处找到有关此内容的更多信息: Passing data back from view controllers Xcode

但是如果你想使用unwind segue,你必须在之前的viewController上编写unwind方法,而不是你当前的。此外,您还需要将方法performSegue与展开segue的标识符一起使用。

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