微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 tableView didSelectRowAt indexPath iOS 之前准备 segue 调用

如何解决在 tableView didSelectRowAt indexPath iOS 之前准备 segue 调用

我有 3 个 VC - VC1、VC2 和 VC3

我正在创建一个放松转场 -

  1. VC1 是目的地
  2. VC2 是源代码

所以,我在 VC1 中添加了 Marker 功能 -

@IBAction func unwindToHomeViewController(_ sender: UIStoryboardSegue) {}

在 VC2 中我创建了两个变量 -

var userSelectedplacesLatitude: Double = 0
var userSelectedplacesLongitude: Double = 0

将在 tableView 中更新 -

func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
    self.userSelectedplacesLatitude = suggestedplacenames[indexPath.row].geometry.coordinates[1]
    self.userSelectedplacesLongitude = suggestedplacenames[indexPath.row].geometry.coordinates[0]
    print("In didSelectRowAt",userSelectedplacesLatitude,userSelectedplacesLongitude)
}

然后准备转场 -

override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
    let destinationVC = segue.destination as! VC1
    print("In Segue preperation",userSelectedplacesLongitude)
    destinationVC.userSelectedplacesLatitude = self.userSelectedplacesLatitude
    destinationVC.userSelectedplacesLongitude = self.userSelectedplacesLongitude
    destinationVC.reloadWeatherDataStatus = true
}

但是从打印值我看到 prepareforSegue调用早于 didSelectRowAt。 因此我没有在 prepareforsugue 中获得预期值

In Segue preperation 0.0 0.0
In didSelectRowAt 49.3227937844972 31.3202829593814

因此 0.0 0.0 一直在传递给 VC1。我该如何解决这个问题?

解决方法

试试下面的代码,让我知道它是否有效 -

func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
    let destinationVC = VC1()
    destinationVC.userSelectedPlacesLatitude = suggestedPlacenames[indexPath.row].geometry.coordinates[1]
    destinationVC.userSelectedPlacesLongitude = suggestedPlacenames[indexPath.row].geometry.coordinates[0]
    destinationVC.reloadWeatherDataStatus = true
    destinationVC.performSegueWithIdentifier("DestinationSegueName",sender: self)
}

对此答案进行修改,因为有些人可能在创建 VC 实例时遇到问题 -

第 1 步 - 从源(VC1)到目标(VC2)视图控制器创建一个名为“SegueToDestinationVc”的手动转场,并在源视图控制器中编写此代码 -

override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
    if (segue.identifier == "SegueToDestinationVc") {
        let vc = segue.destination as! VC2
        vc.dataToPass = someData
    }
}

func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
    someData = placeName[indexPath.row]
}

第 2 步 - 在目标视图控制器 (VC2) 中有一个名为“dataToPass”的公共属性并使用它。

很高兴为您提供帮助,谢谢。

快乐编码

如果您需要任何其他帮助,请告诉我。

,

您遇到的问题是由于在直接从故事板中的表视图单元格链接的展开转场中出现的。一旦用户点击该行,展开转场就会触发。 didSelectRow(at:) 函数在之后被调用,但为时已晚;您已经回到 VC1。

虽然您可以使用prepareForSegue向VC1发送数据,但更好的方法是使用传递给sender的{​​{1}}让VC1获取数据来自 VC2。

这意味着 VC2 不需要了解 VC1 的任何信息。您也可以去掉 unwindToHomeViewController 属性,只需在 unwind 函数中重新加载天气数据状态。

你应该:

  • 从 VC2 中的表视图行中删除 segue
  • 在故事板中,按住 VC2 顶部的“视图控制器”图标拖动到 VC2 顶部的“退出”图标,然后选择 reloadWeatherDataStatus
  • 选择新创建的 unwind segue 并为其指定一个标识符,例如 unwindToHomeViewController
  • 在 VC2 中你有
unwindToVC1
  • 从 VC2 中删除 func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) { self.userSelectedPlacesLatitude = suggestedPlacenames[indexPath.row].geometry.coordinates[1] self.userSelectedPlacesLongitude = suggestedPlacenames[indexPath.row].geometry.coordinates[0] self.performSegue(withIdentifier:"unwindToVC1",sender: self) }

  • 在 VC1 中

prepare(for segue: sender:)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。