1

I'm currently working on creating an iOS app from scratch without using the story board. Everything is working fine except one thing that I can't push the view controller from the cell within the rootViewController, flow is down below, please mention without using storyboard, this project is something I wanna achieve without the storyboard:)

1, creating root view controller in AppDelegate.swift

let layout = UICollectionViewFlowLayout()
let homeViewController = HomeViewController(collectionViewLayout: layout)
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = UINavigationController(rootViewController: homeViewController)
return true

2,creating four cells as within the collection view controller file

collectionView?.register(HomeController.self, forCellWithReuseIdentifier: sections[0])
collectionView?.register(PortfolioController.self, forCellWithReuseIdentifier: sections[1])
collectionView?.register(ConversationController.self, forCellWithReuseIdentifier: sections[2])
collectionView?.register(ProfileController.self, forCellWithReuseIdentifier: sections[3])

3, i have another collection view which has dynamic number of the cells from my api model file , and those name above are with controller but they are collection view cell, i somehow named controller

class ConversationController: BaseCell, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate {

lazy var collectionview: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.dataSource = self
    cv.delegate = self
    cv.backgroundColor = UIColor.white
    return cv
}()

4, registering another cell for this Conversation.swift, and so far everything is working the way i wanted

override func setUpViews() {
    super.setUpViews()
    fetchMessage()
    addSubview(collectionview)
    addConstraintsWithFormat(format: "H:|[v0]|", views: collectionview)
    addConstraintsWithFormat(format: "V:|[v0]|", views: collectionview)
    collectionview.register(ConversationView.self, forCellWithReuseIdentifier: cellId)
}

What I want to achieve is that every time user tap on the ConversationCell which is the ConversationController I want to push the view controller using pushViewController method in the UINavigationController. I know this class doesn't have the super class of UICollectionViewController or UIViewController so what I tried was that because the HomeViewController(at section 1) is UICollectionViewController, I created the reference of HomeViewController within the ConversationController and made the method for pushing the viewController in HomeViewController. I set the break point and it was running but the view didn't get pushed. I wan a bit confused what was going on. Any help or advice will be really helpful! Thank you:)

Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
Kousuke Kuzuoka
  • 125
  • 1
  • 10
  • Just a silly thing to check, as I missed it many of the time. Just check the Navigation controller has initialized or its coming as nil, where you are trying to push. – Janmenjaya Sep 28 '16 at 03:15
  • Can you not just push the view in `didSelectCellAtIndexPath`? Otherwise you need to create a protocol and set your collection view controller as the cells delegate. When the cell is tapped it can fire the delegate method to notify the view controller – Paulw11 Sep 28 '16 at 03:26
  • I added the method which calls HomeViewController's method self.navigationController.pushViewController(theDestination) in didSelectItemAtIndexPath since this ConversationController isnt viewController there is no way to push view controller from view right? i set the break point in didSelectItemAtIndexPath and HomeViewController's method also. Both of them are running correctly the thing is its not pushing the view. Thank you for you suggestion! – Kousuke Kuzuoka Sep 28 '16 at 03:42

0 Answers0