从CollectionViewCell

时间:2017-07-28 18:39:30

标签: swift

这是我想要做的事情。

class UserProfileHeader: UICollectionViewCell {

    lazy var button: UIButton = {
        let button = UIButton(type: .system)
        //...
        button.addTarget(self, action: #selector(handleButtonTapped), for: touchUpInside)
        return button
    }()

    func handleButtonTapped() {
        print("action working now")
        // How to push another vc here?
    }

    // ...
}

我想调用一个navigationController来在collectionViewCell中推送(显示)另一个ViewController。我怎么去那里?我试过了,没有工作。是因为MVC模式吗?细胞不能与他人交谈?或者有什么方法可以做到吗?喜欢使用委托/协议?我对这些事情一无所知。

1 个答案:

答案 0 :(得分:0)

创建一个自定义委托并设置为其viewcontroller,然后当你点击collectionview单元格按钮调用该委托方法时,它将在你的viewcontroller中调用,你可以推送视图控制器

func handleButtonTapped() {
 //Your delegate method showuld be invoked here
 delegate?.buttontappFromCollectionViewCell()

 }

//Now inside your view controller 
 func buttontappFromCollectionViewCell() {
let vc = UIStoryboard(name: "storyboardName", bundle: nil).instantiateViewController(withIdentifier: "yourViewControllerIdentifier")
let navigationController = UINavigationController(rootViewController: vc)
navigationController.pushViewController(vc, animated: true)
}