iphone - 如何在 didSelectItemAt 中发送 segue

标签 iphone swift core-data uicollectionview segue

我正在尝试制作一个 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/

相关文章:

ios - 检测整个屏幕上的触摸

iphone - 是否可以在 ScrollView 中制作单选按钮

ios - Web View 中的 Html5 播放器在 iOS 10 中的 native 播放器中打开视频

ios - NSFetchedResultsControllerDelegate在合并来自另一个上下文的更改后更新表单元格

iphone - 为什么 XCode 显示启动图像警告?

iphone - CGBitmapContextCreateImage 错误

ios - 增加、更新和保存所有单元格 UICollectionView 中的分数

ios - 如何使用 AVPlayer addperiodictimeobserverforinterval 函数更新 uilabel?

ios - 不会处理从这里抛出的错误,因为 context.save 中的封闭捕获并不详尽

swift - 使用 NSTreeController 过滤 NSOutlineView 的子级