首页 > 解决方案 > 按下按钮时推送新的视图控制器

问题描述

我正在以编程方式在 Swift 5 中构建游戏,并希望从主菜单导航到游戏屏幕。这是我的代码:

func handleButtonsTapped() {
    playButton.addTarget(self, action: #selector(pushGameViewController), for: .touchUpInside)
}

以及在点击时处理推送视图控制器的选择器:

@objc func pushGameViewController() {
    let destinationVC = GameViewController()
    navigationController?.pushViewController(destinationVC, animated: true)
}

当我在模拟器中点击按钮时,没有任何反应。我也handleButtonsTapped()调用了我的viewDidLoad()函数。

标签: iosswiftuinavigationcontrollerswift4swift5

解决方案


有答案了!

在我的SceneDelegate.swift我设置:

let menuViewController = UINavigationController(rootViewController: MenuViewController())
window?.rootViewController = menuViewController

现在它起作用了!谢谢大家的评论:)


推荐阅读