George Heints
George Heints

Reputation: 1443

Swift Navigate from UICollectionViewCell to UIViewController

I am in trouble! I need to set navigation from UICollectionViewCell (EVRecordingDetailsCollectionViewCell) to UIViewController (EVPersonalDataViewController) programmatically.

i tried this way.. but it say CollectionViewCell has no member 'present'

let showloginViewController = EVPersonalDataViewController()
        let ncShowloginViewController = UINavigationController(rootViewController: showloginViewController)
        self.present(ncShowloginViewController, animated: true, completion: nil)

Upvotes: 0

Views: 312

Answers (1)

ChanOnly123
ChanOnly123

Reputation: 1024

function present(UIViewController, Bool, (() -> Void)) is member of UIViewController, not UICollectionViewCell.

use this way

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "yourCellId", for: indexPath) as! CustomCell
    cell.parentViewController = self
    // your cell setup...
    return cell
}

in your cell class keep a weak reference to your UIViewController

class CustomCell: UICollectionViewCell {

    weak var parentViewController: UIViewController?

    @IBAction func onClickButton(sender: UIButton) {
        let showloginViewController = EVPersonalDataViewController()
        let ncShowloginViewController = UINavigationController(rootViewController: showloginViewController)
        parentViewController?.present(ncShowloginViewController, animated: true, completion: nil)
    }
}

Upvotes: 1

Related Questions