monekypox121
monekypox121

Reputation: 93

not able to launch UICollectionViewController

I am able to launch the FriendsController(UICollectionViewController) from appdelegate with the following code:

window = UIWindow(frame: UIScreen.main.bounds)window?.makeKeyAndVisible()


    let layout = UICollectionViewFlowLayout()
    let friendsController = FriendsController(collectionViewLayout:  layout)
    window?.rootViewController = UINavigationController(rootViewController: friendsController)

Here is what FriendsController.swift looks like, you can try it yourself and see that it does work:

https://gist.github.com/naderahmedtao/c64d09a3ca62549a015d8df62842b53f

But, now I want to launch FriendsController from a ViewController but nothing happens.

Here is what I have tried:

let friendsController = FriendsController()
        self.navigationController?.pushViewController(friendsController, animated: true)

I also tried this:

    let layout = UICollectionViewFlowLayout()
        let controller = FriendsController(collectionViewLayout: layout)
   navigationController?.pushViewController(controller, animated: true)

I expected FriendsController to launch from the VC, but it does not, it only works from appdelegate.

UPDATE: I just verified that navigationController is nil ! However, I do not want to launch the hosting VC from appdelegate because it is attached to the storyboard... is there any solution?

Upvotes: 1

Views: 72

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

You need to set a navigation for the first vc in AppDelegate for navigationController? not to be nil like

let vc = FirstVC()
window?.rootViewController = UINavigationController(rootViewController: vc)

After that do

let friendsController = FriendsController(collectionViewLayout:UICollectionViewFlowLayout())
navigationController?.pushViewController(controller, animated: true)

Select the vc , then from Editor

enter image description here


You can also load it like

 let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FirstID") as! FirstVC
 window?.rootViewController = UINavigationController(rootViewController: vc)

Upvotes: 1

Related Questions