在另一个 View Controller 中使用变量
Using variables from one View Controller in another
我正在学习有关应用程序开发的 Udemy 教程,并开发了一款井字游戏。我非常喜欢我在应用程序开发方面的时间,并且使用 swift 以外的其他几种语言进行编码。当我允许用户在一个屏幕上输入 his/her 名称(如下面第一张图片所示)并且当 "Play" 按钮被点击时,它会保存输入到文本字段中的名称并连接它在第二个视图控制器上带有“轮到”,以说明轮到哪个用户。在游戏结束时,我还希望显示获胜者的名字。如果我弄清楚如何在第二个视图控制器中使用第一个视图控制器中的用户数据,我将能够自己弄清楚编码部分。我已经提到: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")
我在这里也看到了一个类似的问题,它指出变量是 public 并且可以在整个过程中使用,我的问题是我希望在按下按钮并声明它们时保存名称 public/static也没有用。
我建议第 3 个 class 抽象玩家信息并使其成为单例。像这样:
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")
}
}
}
和access/set一样:
Players.shared.one = "Name one"
print(Players.shared.one)
我正在学习有关应用程序开发的 Udemy 教程,并开发了一款井字游戏。我非常喜欢我在应用程序开发方面的时间,并且使用 swift 以外的其他几种语言进行编码。当我允许用户在一个屏幕上输入 his/her 名称(如下面第一张图片所示)并且当 "Play" 按钮被点击时,它会保存输入到文本字段中的名称并连接它在第二个视图控制器上带有“轮到”,以说明轮到哪个用户。在游戏结束时,我还希望显示获胜者的名字。如果我弄清楚如何在第二个视图控制器中使用第一个视图控制器中的用户数据,我将能够自己弄清楚编码部分。我已经提到: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")
我在这里也看到了一个类似的问题,它指出变量是 public 并且可以在整个过程中使用,我的问题是我希望在按下按钮并声明它们时保存名称 public/static也没有用。
我建议第 3 个 class 抽象玩家信息并使其成为单例。像这样:
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")
}
}
}
和access/set一样:
Players.shared.one = "Name one"
print(Players.shared.one)