准备不发送数据

时间:2018-06-29 17:37:56

标签: swift firebase uicollectionview

我正在使用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()

当我在prepare函数的最后一行放置一个断点时,它会被命中,而控制台在CityViewController处放置一个断点时会正确获取值,这给了我一个空的var。 然后我检查了代码和我的名字,一切看起来都很好!

1 个答案:

答案 0 :(得分:0)

正确的设置

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