gpt4 book ai didi

ios - 使用 SpriteKit 传递多个数组 - UIColor 和 String

转载 作者:行者123 更新时间:2023-11-30 11:39:55 24 4
gpt4 key购买 nike

我有三个数据 View Controller :settingsVC.swift、menuVC.swift 和 GameScene.swift

我正在尝试使用prepareforsegue方法将[UIColor]和[String]数组从设置传输到menuVC,然后使用instantiateViewController从menuVC到GameScene.swift的另一次传输。我已在每个数组中声明了空数组,只想将数据从一页传递到另一页,并且在我的两个数组上都收到“使用未解析的标识符”。如有任何帮助,我们将不胜感激。

class MenuVC: UIViewController {

var colourSettingsMenu = [UIColor]()
var otherSettingsMenu = [String]()

//action function for button to transfer to a 1 player game
@IBAction func Player1Game(_ sender: MenuVC) {
moveToGame(game: .player1, colourSettings: colourSettingsMenu, otherSettings: otherSettingsMenu)
}

//action function for button to transfer to a 2 player game
@IBAction func Player2Game(_ sender: MenuVC) {
moveToGame(game: .player2, colourSettings: colourSettingsMenu, otherSettings: otherSettingsMenu)
}


func moveToGame(game : gameType, colourSettings: [UIColor], otherSettings: [String]) {
let GameVC = self.storyboard?.instantiateViewController(withIdentifier: "GameVC") as! GameViewController

currentGameType = game
colourSettingsGame.GameVC = colourSettings;
otherSettingsGame.GameVC = otherSettings;
self.navigationController?.pushViewController(GameVC, animated: true)
}

@IBAction func moveToSettings(_ sender: Any) {
performSegue(withIdentifier: "settings", sender: self)

}

override func prepare(for segue: UIStoryboardSegue, sender:Any?) {
if (segue.identifier == "settings") {
let SettingsVC = segue.destination as! SettingsVC
SettingsVC.colourSettings = colourSettingsMenu
SettingsVC.otherSettings = otherSettingsMenu
}

}

}

class SettingsVC : UIViewController {

//Colours for segue transfer
var player1Colour: UIColor?
var player2Colour: UIColor?
var ballColour: UIColor?

// other settings to allow segue transfer
// do we need these
var AIDiff: String?
var paddleSi: String?
var ballSi: String?

//arrays for segue transfer
var colourSettings = [UIColor]()
var otherSettings = [String]()

override func prepare(for segue: UIStoryboardSegue, sender:Any?) {
let colourSettings = [player1Colour, player2Colour, ballColour]
let otherSettings = [AIDiff, paddleSi, ballSi]

if (segue.identifier == "back") {
let MenuVC = segue.destination as! MenuVC
MenuVC.colourSettingsMenu = colourSettings as! [UIColor]
MenuVC.otherSettingsMenu = otherSettings as! [String]
}

}

class GameScene: SKScene {

// init colour settings array
var colourSettingsGame = [UIColor]()
// init other settings array
var otherSettingsGame = [String]()

var currentGameType = gameType?.self
}

最佳答案

这里你不应该像类名一样命名变量

override func prepare(for segue: UIStoryboardSegue, sender:Any?) {
if (segue.identifier == "settings") {
let nextSettings = segue.destination as! SettingsVC
nextSettings.colourSettings = colourSettingsMenu
nextSettings.otherSettings = otherSettingsMenu
}

}

这里

override func prepare(for segue: UIStoryboardSegue, sender:Any?) {
let colourSettings = [player1Colour, player2Colour, ballColour]
let otherSettings = [AIDiff, paddleSi, ballSi]

if (segue.identifier == "back") {
let nexrMenu = segue.destination as! MenuVC
nexrMenu.colourSettingsMenu = colourSettings as! [UIColor]
nexrMenu.otherSettingsMenu = otherSettings as! [String]
}

关于ios - 使用 SpriteKit 传递多个数组 - UIColor 和 String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49366943/

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