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

时间:2019-11-17 15:22:32

标签: swift xcode

我已经使用Xcode和Swift 5构建了一个应用。

每次我单击“简单”,“中等”,“硬”或“ 2个播放器”按钮时,都会出现错误:

  

无法将类型为'Pong.MenuVC'(0x1072ea808)的值强制转换为   '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
    }
}

1 个答案:

答案 0 :(得分:0)

在故事板上,ID gameVC 的类型为5 * cos(x) + 3 * sin(x)而不是MenuVC,因此请更改vc的类名

GameViewController