使用未解析的标识符" winnerLabel"

时间:2016-08-26 22:50:42

标签: ios xcode swift3

我得到了几个不同变量的未解析标识符。我在viewController类中定义了变量,但出于某种原因,当我将它们添加到viewDidLoad()函数时,它有一个错误。有什么建议?提前致谢!我在新的Xcode v8 beta 6中使用Swift3

导入UIKit

class ViewController: UIViewController {    
    @IBOutlet weak var playAgainButton: UIButton!
    @IBOutlet weak var winnerLabel: UILabel!
    @IBAction func playAgain(_ sender: AnyObject) {
    }
    //1 is noughts, 2 is crosses
    var activeGame = true
    var activePlayer = 1
    var gameState = [0, 0, 0, 0, 0, 0, 0, 0, 0] //0 - empty, 1 - noughts, 2 - crosses
    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: [])
                print(sender.tag)
                activePlayer = 2
            } else {
                sender.setImage(UIImage(named: "cross.png"), for: [])
                print(sender.tag)
                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  winner!
                    activeGame = false
                    print(gameState[combination[0]])
                    if gameState[combination[0]] == 1 {
                        winnerLabel.text = "Noughts has won!"                        
                    } else {                        
                        winnerLabel.text = "Crosses has won!"                        
                    }                    
                    UIView.animate(withDuration: 1, animations: {                        
                        self.winnerLabel.center = CGPoint(x: self.winnerLabel.center.x + 500, y: self.winnerLabel.center.y)
                        self.playAgainButton.center = CGPoint(x: self.playAgainButton.center.x + 500, y: self.playAgainButton.center.y)
                    })
                }                    
            }
        }
    }
}

func viewDidLoad() {
    viewDidLoad()    
    winnerLabel.isHidden = true
    playAgainButton.isHidden = true    
    winnerLabel.center = CGPoint(x: winnerLabel.center.x - 500, y: winnerLabel.center.y)
    playAgainButton.center = CGPoint(x: playAgainButton.center.x - 500, y: playAgainButton.center.y)   
}

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

1 个答案:

答案 0 :(得分:0)

检查你的花括号。在viewDidLoad之前你有太多了,那么你最后需要一个关闭课程。

此外,viewDidLoad()didReceiveMemoryWarning()函数应该在它们之前使用关键字覆盖:

override func viewDidLoad() {