我正在尝试制作一个 segue,将字符串发送到下一个 View Controller 中的变量。此字符串是 Collection View 单元格内标签内的文本,标签内的文本来自 CoreData。当用户按下单元格时,它意味着转到下一个 View Controller ,并让先前在变量中选择的单元格文本。我不知道该怎么做,这是我的 didSelect 函数,它能够获取所选单元格的文本:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellP", for: indexPath) as! CCCollectionViewCell
let project = projectList[indexPath.row]
if project.name! == "Hi " {
print("Yes")
}
else {
print("no")
}
print(project.name!)
}
问题是我不确定如何将 segue 中所选单元格的这个值(project.name!)发送到下一个 View Controller 变量。
最佳答案
这样试试。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! NextViewController //Your ViewController class
if let cell = sender as? UICollectionViewCell,
let indexPath = self.collectionView.indexPath(for: cell){
let project = projectList[indexPath.row]
print(project.name)
vc.name = project.name
}
}
现在只需在您的 NextViewController
中创建一个 String
类型的属性。
var name = String()
关于iphone - 如何在 didSelectItemAt 中发送 segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42896493/