美文网首页
Swift开发笔记2 - 穿透view层级传值VC

Swift开发笔记2 - 穿透view层级传值VC

作者: feb961880dc1 | 来源:发表于2020-04-28 15:29 被阅读0次

开发中遇到了view层级太多难以传值当前vc:

vc -> tableView -> cell -> collectionView -> cell

1、创建协议

protocol TestCellDelegate {
    func didSelect(model:TestModel)
}

2、vc 遵守协议

class TestViewController: TestCellDelegate {
   func didSelect(model:TestModel) {
     print(model)
  }
}

3、collectionViewDelegate里获取当前的vc,执行方法

extension TestTableViewCell : UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let vc = self.getCurController() as? TestCellDelegate, let model = self.list[indexPath.row,true] {
            vc.didSelect(model: model)
        }
    }
}

相关文章

网友评论

      本文标题:Swift开发笔记2 - 穿透view层级传值VC

      本文链接:https://www.haomeiwen.com/subject/juovwhtx.html