【问题标题】:Prepare for segue not passing data准备 segue 不传递数据
【发布时间】:2018-06-29 17:37:55
【问题描述】:

我正在使用 firebase 开发一个 Swift 项目,我有一个包含所有类别的 UICollectionViewController,我需要将类别 ID 传递给下一个视图,但它传递了一个空字符串

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let category = cnameArray[indexPath.row]
    let catId = category.caId
    func prepare(for segue: UIStoryboardSegue, sender: Any?){
        if segue.identifier == "CatCity" {
        let cityView = segue.destination as? CityViewController
            cityView?.catId = catId!
    } }
    self.performSegue(withIdentifier: "CatCity", sender: self)

}

在我的 CityViewController 我有

    var catId = String()

当我在准备函数的最后一行放置一个断点时,它会被命中,并且控制台会正确获取值,而在 CityViewController 处设置断点会给我一个空变量。 我检查了代码和我的 segue 名称,一切看起来都很好!

【问题讨论】:

  • 我不认为你的准备函数应该在 didselectIteAt 中。

标签: swift firebase uicollectionview


【解决方案1】:

正确设置

 func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let category = cnameArray[indexPath.row]
    self.performSegue(withIdentifier: "CatCity", sender: category.caId)
}

func prepare(for segue: UIStoryboardSegue, sender: Any?){
   if segue.identifier == "CatCity" {
      if let cityView = segue.destination as? CityViewController {
        cityView.catId = sender as! String
      } 
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多