Chakeeel
Chakeeel

Reputation: 165

didSelectItemAt IndexPath not pushing controller (but does get called)

I have made a request to push navigationController to a new viewController when an item in my UICollectionView has been selected. The code runs but the action isn't performing. didSelectItemAt is being called however, as I checked this by requesting the colour changes when selected (which executes). The idea is to click on someone who has sent a message (chat function) and be sent to the collection of messages they have sent me. This project is done programmatically! Any ideas?

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let layout = UICollectionViewFlowLayout()
    let controller = ChatLogController(collectionViewLayout: layout)
    controller.match = messages?[indexPath.item].match
    navigationController?.pushViewController(controller, animated: true)


    let cell : UICollectionViewCell = collectionView.cellForItem(at: indexPath)!
    cell.backgroundColor = UIColor.magenta
}

for further context, here is my AppDelegate's didFinishLaunchingWithOptions:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    window = UIWindow()
    window?.makeKeyAndVisible()
    window?.rootViewController = MatchesController()

    return true
}

Upvotes: 0

Views: 148

Answers (2)

Alastar
Alastar

Reputation: 1374

edit based on your comment below:

since it's created programmatically so you need to include you MatchesController in a navigation stack:

so in your didFinishLaunchingWithOptions:

let navigationController = UINavigationController(rootViewController: MatchesController())

window?.rootViewController = navigationController
window?.makeKeyAndVisible()

then you can push your ChatLogController from your MatchesController()

Upvotes: 1

Rizwan Ahmed
Rizwan Ahmed

Reputation: 958

Can you please check whether "navigationController" is not nil? (One more thing pushViewController is different from performing segue.)

Upvotes: 0

Related Questions