【问题标题】:How to Send a segue in didSelectItemAt如何在 didSelectItemAt 中发送 segue
【发布时间】:2017-03-20 05:36:18
【问题描述】:

我正在尝试制作一个将字符串发送到下一个视图控制器中的变量的 segue。此字符串是集合视图单元格内的标签内的文本,标签内的文本来自 CoreData。当用户按下单元格时,它意味着转到下一个视图控制器,并将先前选择的单元格的文本放在一个变量中。我不知道该怎么做,这是我的 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!)

}

问题是我不确定如何将选定单元格的这个值(project.name!)在 segue 中发送到下一个视图控制器变量。

【问题讨论】:

  • 当你选择单元格时你的segue是否执行意味着你能移动到nextViewController吗?
  • 从不cellForItemAtIndexPath之外打电话给dequeueReusableCell。从数据源数组中获取数据。

标签: iphone swift core-data uicollectionview segue


【解决方案1】:

这样试试。

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 2020-01-13
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    相关资源
    最近更新 更多