swift - 如何更新不同视图控制器中的标签?
问题描述
我对编码很陌生。我创建了一个游戏,scoreLabel
在GameViewController
. 我在另一个ShopViewController
名为moneyLabel
. 我希望钱标签等于分数。我这样做是为了让我的分数标签在你每次开始新游戏时都会重置,但我希望我的钱保持不变,这样我就可以用这笔钱在游戏中购买。
我不知道如何让它更新。我试过:gameViewController = GameViewController()
然后使用代码更新我的标签,但它不起作用。我的标签根本没有更新。它保持在 0。
解决方案
我认为您必须使用一些模型结构,您可以在其中添加分数或任何其他数据并轻松操作它:
struct GameData{
var totalScore: Int = 0
var currentGameScore: Int = 0
func invalidateCurrentScore(){
totalScore += currentGameScore
currentGameScore = 0
//call this method when you need to set your current score to 0, and pass all the accumulated points to your total score (f.ex. in the end of the game)
}
//some other data like player's name and etc...
}
您还需要创建一个委托协议,以便在 ShopViewController 中使用其方法并将游戏数据返回给 GameViewController:
protocol ShopViewControllerDelegate{
func updateGameData(by gameData: GameData)
}
带有新部件的 GameViewController:
class GameViewConroller: UIViewController, ShopViewControllerDelegate{
@IBOutlet weak var scoreLabel: UILabel!
var gameData: GameData!
override func viewDidLoad(){
super.viewDidLoad()
gameData = GameData()
updateScoreLabel()
}
private func updateScoreLabel(){ //call it anyTime when you manipulate the score of current game
scoreLabel.text = "Score: \(gameData.currentGameScore)"
}
func updateGameData(by gameData: GameData){ //This is the delegate's method that ShopViewController needs to trigger when you buy something in the shop
self.gameData = gameData
updateScoreLabel()
}
override func prepare(for segue: UIStoryboardSegue,
sender: Any?){
if let shopViewController = segue.destination as? ShopViewController{
//passing game data to ShopViewController
shopViewController.gameData = self.gameData
shopViewController.delegate = self //!IMPORTANT
}
}
}
这些是您的 ShopViewController 应该具有的一些属性和方法:
class ShopViewController{
@IBOutlet weak var totalScoreLabel: UILabel!
var gameData: GameData!
var delegate: ShopViewControllerDelegate?
override func viewDidLoad(){
super.viewDidLoad()
updateTotalScoreLabel()
}
private func updateTotalScoreLabel(){
totalScoreLabel.text = "Total Score: \(gameData.totalScore + gameData.currentGameScore)"
}
private func purchasingSomething(){
//logic of getting price of an item
gameData.currentGameScore -= price
if gameData.currentGameScore < 0{
gameData.totalScore += gameData.currentGameScore
gameData.currentGameScore = 0
//if your current score is less than 0 after some purchase, your total score decreases and the current score becomes 0
}
updateTotalScoreLabel()
delegate?.updateGameData(by gameData: gameData) //Pass refreshed gameData back to the GameViewController
}
}
Swift 也有静态变量,但它非常易于使用。代表带来更多乐趣并使代码更漂亮。
struct GameData{
static var totalScore: Int = 0
static var currentGameScore: Int = 0
}
祝你好运,玩得开心游戏开发:)
推荐阅读
- c# - 在字符串中将 Not 替换为 And Not
- reactjs - TSX 组件数组,数组内的组件
- magento2.1 - 如何在 Magento-2.1.12 中单独检查每个项目的订单状态?
- javascript - 一个标签的href javascript函数,如何引用调用者标签
- .net - 如何在 Azure 中获取 Windows 10 vm 的用户登录状态
- intellij-idea - 如何将外部 JAR 从一个项目导入到另一个项目?
- html - 用css重新设计两个相邻的div
- selenium - 使用 Python Selenium 滚动页面
- python - 在发出 CTRL+C 命令之前打开读取数据
- google-cloud-storage - 为存储桶中的每个文件运行 Google Cloud 函数