如何从UIView推送视图控制器

Eri*_*sta 2 ios uicollectionview swift

当我点击一个单元格时,我想推送这个视图控制器.

这是我的代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let user = filteredUsers[indexPath.item]
    print(user.username)

    let userProfileController = UserProfileController(collectionViewLayout: UICollectionViewFlowLayout())

}
Run Code Online (Sandbox Code Playgroud)

我想推userProfileController.

注意:这UIView不是视图控制器

Mic*_*ień 7

你不能从UIView推出任何控制器.要做到这一点,你必须使用NavigationController.

我假设你在一些UIViewController中有你的UIView,所以很多选项之一就是创建一个委托,告诉你的视图控制器进行推送.

protocol MyViewDelegate {
    func didTapButton()
}

class MyView: UIView {

    weak var delegate: MyViewDelegate?

    func buttonTapAction() {
        delegate?.didTapButton()
    }
}

class ViewController: UIViewController, MyViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        myView.delegate = self
    }

    func didTapButton() {
        self.navigationController?.pushViewController(someVc, animated: true)
    } 

}
Run Code Online (Sandbox Code Playgroud)