gpt4 book ai didi

ios - 通过发送 "ID"作为参数,将数据从一个 collectionView 单元格传递到 swift 4 中的其他 Collection View Controller

转载 作者:行者123 更新时间:2023-11-29 06:01:33 26 4
gpt4 key购买 nike

我对 Swift 4 中的 Collection View 有疑问。

我有一个 Collection View ,将其命名为Category-Collection-View-Controller。在此,每个类别都有一个来自后端的唯一 ID。当我单击特定类别时,它将导航到另一个名为 SubCategories-CollectionView-Controller 的 Collection View 。并显示“Category-Collection-View-Controller”的所有信息。请任何人向我解释一下如何将来自后端的“ID”作为 Alamofire 函数中的参数传递。

最佳答案

您可以通过以下方式将数据从一个 View Controller 传递到另一个 View Controller :

  • 代表
  • 继续
  • 通知

我建议您使用segue,因为您必须在类别点击上执行segue操作。

编码示例:

  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

// You can use a traditional push view controller method.
//let id = dataSourceArray[indexPath.item]
//let vc = self.storyboard?.instantiateViewController(withIdentifier: "SubCategories-CollectionView-Controller"") as! SubCategories-CollectionView-Controller
//vc.id = id
// self.navigationController?.pushViewController(vc, animated: false)

// Get the category id and pass it through segue
performSegue(withIdentifier: "identifier", sender: id)
}

并且在prepare for segue方法中,您可以将此ID传递给下一个 View Controller 。

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "identifier" {
let senderID = sender as? Int
let vc = segue.destination as? SubCategories-CollectionView-Controller
vc.id = senderID
}
}

关于ios - 通过发送 "ID"作为参数,将数据从一个 collectionView 单元格传递到 swift 4 中的其他 Collection View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54629086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com