gpt4 book ai didi

swift - 无法将类型 'Pong.MenuVC' (0x1072ea808) 的值转换为 'Pong.GameViewController'

转载 作者:行者123 更新时间:2023-11-30 10:32:13 25 4
gpt4 key购买 nike

我使用 Xcode 和 swift 5 构建了一个应用程序。

每次我点击“简单”、“中等”、“困难”或“2 人”按钮时,我都会收到错误消息:

Could not cast value of type 'Pong.MenuVC' (0x1072ea808) to 'Pong.GameViewController'

有谁知道怎么解决吗?

感谢您的帮助。

MenuVC代码:

enum gameType {
case easy
case medium
case hard
case player2
}

class MenuVC : UIViewController {

@IBAction func Player2(_ sender: Any) {
moveToGame(game: .player2)
}

@IBAction func Easy(_ sender: Any) {
moveToGame(game: .easy)
}

@IBAction func Medium(_ sender: Any) {
moveToGame(game: .medium)
}

@IBAction func Hard(_ sender: Any) {
moveToGame(game: .hard)
}
```

**Code with Thread 1: signal SIGABRT:**

```swift
func moveToGame(game : gameType) {
let gameVC = self.storyboard?.instantiateViewController(withIdentifier: "gameVC") as! GameViewController

currentGameType = game

self.navigationController?.pushViewController(gameVC, animated: true)
}
}

GameViewController代码:

import UIKit
import SpriteKit
import GameplayKit

var currentGameType = gameType.medium

class GameViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
if let scene = SKScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill

scene.size = view.bounds.size

// Present the scene
view.presentScene(scene)
}

view.ignoresSiblingOrder = true

view.showsFPS = true
view.showsNodeCount = true
}
}

override var shouldAutorotate: Bool {
return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
if UIDevice.current.userInterfaceIdiom == .phone {
return .allButUpsideDown
} else {
return .all
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}

override var prefersStatusBarHidden: Bool {
return true
}
}

最佳答案

在 Storyboard中,id gameVC 的类型为 MenuVC 而不是 GameViewController,因此请更改 vc 的类名

let gameVC = self.storyboard?.instantiateViewController(withIdentifier: "gameVC") as! GameViewController

关于swift - 无法将类型 'Pong.MenuVC' (0x1072ea808) 的值转换为 'Pong.GameViewController',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58901974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com