【问题标题】:How do I update a label in a different view controller?如何更新不同视图控制器中的标签?
【发布时间】:2020-04-29 07:53:05
【问题描述】:

我对编码很陌生。我创建了一个游戏,在GameViewController 的上角有一个scoreLabel。我在另一个名为 moneyLabel 的视图控制器 (ShopViewController) 中创建了另一个标签。我希望钱标签等于分数。我这样做是为了在您每次开始新游戏时重置我的分数标签,但我希望我的钱保持不变,以便我可以将这些钱用于购买游戏。

我不知道如何让它更新。我试过:gameViewController = GameViewController(),然后使用代码更新我的标签,但它不起作用。我的标签根本没有更新。它保持在 0。

【问题讨论】:

  • 您可以通过调用它来更改以前的视图控制器的标签。您需要创建一个协议来更改以前的视图控制器(GameViewController)的标签,例如medium.com/@ales.musto/…
  • 你可以使用NotificationCenter

标签: swift swift5


【解决方案1】:

我认为您必须使用一些模型结构,您可以在其中添加分数或任何其他数据并轻松操作它:

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
}

祝你好运,与游戏开发者一起玩得开心:)

【讨论】:

    猜你喜欢
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 2021-08-09
    • 2011-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多