在另一个View Controller中使用变量

时间:2018-06-29 01:46:41

标签: ios swift swift4 xcode9

我正在关注有关应用程序开发的Udemy教程,并致力于Tic Tac Toe游戏。我绝对热爱我的时间用于应用程序开发,并且除了swift之外还使用其他几种语言进行了编码。当我允许用户在一个屏幕上输入他/她的名字时(第一个图像如下所示),当单击“播放”按钮时,它会保存键入到文本字段中的名称并将其连接为“'” s turn”(第二个视图控制器上的“ s turn”),以说明哪个用户打开了它。在游戏结束时,我还希望显示获胜者的名字。如果我在第二个视图控制器中找到了如何使用用户数据的方式,我将能够自行找出编码部分。我已引用:Accessing variables from another ViewController in Swift,但我仍然不了解它是如何工作的,或者是否与我遇到相同的问题。我将在下面附上两个视图控制器及其代码的图片。我在这里先向您的帮助表示感谢!

两个视图控制器

代码输入到需要用户输入的视图控制器:

// MainViewController.swift
// Tic Tac Toe
// main menu/player select

import UIKit

class MainViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var playerOneName: UITextField!
    @IBOutlet weak var playerTwoName: UITextField!

    @IBAction func play(_ sender: Any) {

    }

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()

        return true
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }

}

实际游戏代码:

// ViewController.swift
// Tic Tac Toe
// View Controller game screen

import UIKit

class ViewController: UIViewController {

    // 1 is noughts, 2 is crosses
    var activePlayer:Int = 1
    var activeGame:Bool = true
    @IBOutlet weak var turnLabel: UILabel!
    var gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0] // 0 = empty, 1 = nought, 2 = cross
    let winningCombination = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8],[0, 4, 8], [2, 4, 6]]

    @IBAction func buttonPressed(_ sender: AnyObject) {
        let activePosition = sender.tag - 1

        if gameState[activePosition] == 0  && activeGame{
            gameState[activePosition] = activePlayer

            if activePlayer == 1 {
                (sender).setImage(UIImage(named: "nought.png"), for: [])
                activePlayer = 2
            } else {
                (sender).setImage(UIImage(named: "cross.png"), for: [])
                activePlayer = 1
            }

            for combination in winningCombination {
                if gameState[combination[0]] != 0 && gameState[combination[0]] == gameState[combination[1]] && gameState[combination[1]] == gameState[combination[2]] {
                    // We have a winner!
                    activeGame = false
                    print(gameState[combination[0]])
                }
            }
        }// the spot will only be taken by the player if it is not taken yet

    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

还请注意,我曾尝试使用以下方法将数据保存在用户设备上:

UserDefaults.standard.set(playerOneName.text forKey: "playerone")

我在这里也看到了一个类似的问题,指出变量是公共变量,并且可以在整个变量中使用,我的问题是我希望在按下按钮时保存名称,并且声明它们在公共/静态位置也不起作用。

1 个答案:

答案 0 :(得分:0)

我建议使用第3类来抽象播放器信息并使之成为Singleton。像这样:

class Players {
    static let shared = Players()

    var one: String {
        get {
            return UserDefaults.standard.object(forKey: "playerone") as? String ?? ""
        }
        set(newValue) {
            UserDefaults.standard.set(newValue, forKey: "playerone")
        }
    }
}

并像这样访问/设置:

Players.shared.one = "Name one"
print(Players.shared.one)