按下按钮时推送新的视图控制器
Push new view controller when button is pressed
我正在 Swift 5 中以编程方式构建游戏,并希望从主菜单导航到游戏屏幕。这是我的代码:
func handleButtonsTapped() {
playButton.addTarget(self, action: #selector(pushGameViewController), for: .touchUpInside)
}
以及在点击时处理推动视图控制器的选择器:
@objc func pushGameViewController() {
let destinationVC = GameViewController()
navigationController?.pushViewController(destinationVC, animated: true)
}
当我在模拟器中点击按钮时没有任何反应。我也在 viewDidLoad()
函数中调用了 handleButtonsTapped()
。
这是我的代码,可以运行。
你的 ViewController 设置为 UINavigation 了吗?
@IBOutlet weak var playButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
handleButtonsTapped()
}
func handleButtonsTapped() {
playButton.addTarget(self, action: #selector(pushGameViewController), for: .touchUpInside)
}
@objc func pushGameViewController() {
let destinationVC = GameViewController()
navigationController?.pushViewController(destinationVC, animated: true)
}
得到答案了!
在我的 SceneDelegate.swift
我设置:
let menuViewController = UINavigationController(rootViewController: MenuViewController())
window?.rootViewController = menuViewController
现在可以了!感谢大家的评论:)
您不能直接初始化故事板视图控制器插座。相反,您可以从发生故事板的包中加载它。按照以下步骤操作。
- 首先设置Storyboard Id 'GameViewController'(根据截图)
然后将 pushGameViewController 函数替换为下面的函数。
@objc func pushGameViewController() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationVC = storyboard.instantiateViewController(withIdentifier: "GameViewController")
self.navigationController?.pushViewController(destinationVC, animated: true)
}
@objc func pushGameViewController() {
let destinationVC = storyboard.instantiateViewController(withIdentifier: "GameViewController") as! GameViewController
navigationController?.pushViewController(destinationVC, animated: true)
}
请务必在界面构建器中为您的视图控制器提供一个名为 GameViewController
的故事板 ID
我正在 Swift 5 中以编程方式构建游戏,并希望从主菜单导航到游戏屏幕。这是我的代码:
func handleButtonsTapped() {
playButton.addTarget(self, action: #selector(pushGameViewController), for: .touchUpInside)
}
以及在点击时处理推动视图控制器的选择器:
@objc func pushGameViewController() {
let destinationVC = GameViewController()
navigationController?.pushViewController(destinationVC, animated: true)
}
当我在模拟器中点击按钮时没有任何反应。我也在 viewDidLoad()
函数中调用了 handleButtonsTapped()
。
这是我的代码,可以运行。
你的 ViewController 设置为 UINavigation 了吗?
@IBOutlet weak var playButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
handleButtonsTapped()
}
func handleButtonsTapped() {
playButton.addTarget(self, action: #selector(pushGameViewController), for: .touchUpInside)
}
@objc func pushGameViewController() {
let destinationVC = GameViewController()
navigationController?.pushViewController(destinationVC, animated: true)
}
得到答案了!
在我的 SceneDelegate.swift
我设置:
let menuViewController = UINavigationController(rootViewController: MenuViewController())
window?.rootViewController = menuViewController
现在可以了!感谢大家的评论:)
您不能直接初始化故事板视图控制器插座。相反,您可以从发生故事板的包中加载它。按照以下步骤操作。
- 首先设置Storyboard Id 'GameViewController'(根据截图)
然后将 pushGameViewController 函数替换为下面的函数。
@objc func pushGameViewController() { let storyboard = UIStoryboard(name: "Main", bundle: nil) let destinationVC = storyboard.instantiateViewController(withIdentifier: "GameViewController") self.navigationController?.pushViewController(destinationVC, animated: true) }
@objc func pushGameViewController() {
let destinationVC = storyboard.instantiateViewController(withIdentifier: "GameViewController") as! GameViewController
navigationController?.pushViewController(destinationVC, animated: true)
}
请务必在界面构建器中为您的视图控制器提供一个名为 GameViewController
的故事板 ID