didSelectItemAt IndexPath不推送控制器(但确实被调用)

时间:2019-05-23 10:24:37

标签: swift uicollectionview

当我的UICollectionView中的一个项目被选中时,我已经请求将NavigationController推到一个新的viewController。代码已运行,但操作未执行。但是,didSelectItemAt正在被调用,因为我通过选择(执行)时请求颜色更改来进行检查。这个想法是单击发送消息的人(聊天功能),然后将其发送给他们发送给我的消息集合。该项目以编程方式完成!有什么想法吗?

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
}

有关进一步的上下文,这是我的AppDelegate的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
}

2 个答案:

答案 0 :(得分:1)

根据您在下面的评论进行编辑:

由于它是通过编程方式创建的,因此您需要将MatchesController包括在导航堆栈中:

所以在您的didFinishLaunchingWithOptions中:

let navigationController = UINavigationController(rootViewController: MatchesController())

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

然后您可以将ChatLogController中的MatchesController()推入

答案 1 :(得分:0)

能否请您检查“ navigationController”是否为nil? (还有另一件事pushViewController与执行segue不同。)