【问题标题】:Could not cast value of type 'UIImageView' (0x17c0f5c) to 'UIButton' (0x17c3298)无法将“UIImageView”(0x17c0f5c)类型的值转换为“UIButton”(0x17c3298)
【发布时间】:2016-04-20 07:41:37
【问题描述】:

我正在尝试重置井字游戏的棋盘,但如果我在 for 循环中将 i 更改为 1,一切正常。我还将标签、按钮和视图的标签从 0 更改为20 但我仍然收到此错误。这是代码

var activePlayer = 1

    var gameActive = true
    var gameState = [0,0,0,0,0,0,0,0,0]  // board is empty
    var winningCombinations = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]


    @IBOutlet var playAgainButton: UIButton!
    @IBOutlet var gameOverLabel: UILabel!
    @IBAction func playAgainPressed(sender: AnyObject) {

         activePlayer = 1
         gameActive = true
        gameState = [0,0,0,0,0,0,0,0,0]

        var button : UIButton
        //resetting the image of the button
        for var i = 0; i < 9; i++ {

            button = view.viewWithTag(i) as! UIButton
            button.setImage(nil, forState: .Normal)
        }


        gameOverLabel.hidden = true
        playAgainButton.hidden = true
        gameOverLabel.center = CGPointMake(gameOverLabel.center.x - 400, gameOverLabel.center.y)
        playAgainButton.center = CGPointMake(playAgainButton.center.x - 400, playAgainButton.center.y)


    }
    @IBOutlet var button: UIButton!
    @IBAction func buttonPressed(sender: AnyObject) {

        if gameState[sender.tag] == 0 && gameActive == true{
        var image = UIImage()
        // updating board

        gameState[sender.tag] = activePlayer

        if activePlayer == 1 {

         image = UIImage(named: "nought.png")!
       activePlayer = 2

        } else {
             image = UIImage(named: "cross.png")!
            activePlayer = 1

        }
               // to access the tag of the image pressed use sender.tag
            sender.setImage(image, forState: .Normal)


            for combination in winningCombinations {


                if gameState[combination[0]] != 0  && gameState[combination[0]] == gameState[combination[1]] && gameState[combination[1]] == gameState[combination[2]]{

                    var labelText = "Noughts have won!"

                    if gameState[combination[0]] == 2{
                        labelText = "Crosses have won"
                    }
                    gameOverLabel.text = labelText

                    gameOverLabel.hidden = false
                    playAgainButton.hidden = false


                    UIView.animateWithDuration(0.5, animations: { () -> Void in
                        self.gameOverLabel.center = CGPointMake(self.gameOverLabel.center.x + 400, self.gameOverLabel.center.y)
                        self.playAgainButton.center = CGPointMake(self.playAgainButton.center.x + 400, self.playAgainButton.center.y)


                    })
                    gameActive = false
                }
            }

     }
}

【问题讨论】:

    标签: xcode swift


    【解决方案1】:

    问题已解决。我这样做的方式是在我的板之后移动带有 0 标签的按钮。

    【讨论】:

      【解决方案2】:

      检查您的情节提要 - 必须有一个 UIImageView,其标签等于 i。视图标签应该是唯一的,尽管没有强制执行这一点的机制 - 这是你需要照顾自己的事情。

      标签的用处是有限的;在大多数情况下,您最好使用IBOutlets。

      如果您想坚持使用标签,请将您的 UIButtons 嵌入到 @IBOutlet UIView buttonContainerView 中,然后替换

      for var i = 0; i < 9; i++ {
          button = view.viewWithTag(i) as! UIButton
          button.setImage(nil, forState: .Normal)
      }
      

      for subview in buttonContainerView.subviews as! UIView {
          if let button = view as? UIButton {
              button.setImage(nil, forState: .Normal)
          }
      }
      

      【讨论】:

      • 我给了他们每个独特的标签。当我将 for 循环更改为 for var i = 1;我
      • 我看到你编辑了你的代码 - 我现在从 0 而不是 1 开始。因为 0 是 UIView.tag 的默认值,所以 view.viewWithTag(i) 可以返回几乎所有的子视图。
      • 但所有子视图标签都不为零
      • 可能是这样,但是viewWithTag 函数会遍历整个视图层次结构——不仅是直接子视图,还有那些子视图等等。所有UIButtons 下面都有一个UIImageViews,所以我认为该方法会选择其中一个。
      【解决方案3】:

      转到情节提要并在视图中将您的零标签按钮移到所有其他按钮上方。如果它不起作用,请再次将其向上移动,因此它是视图中层次结构中的第一项。这应该允许代码先找到零标签按钮,然后再找到其他带有零标签的东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-23
        • 1970-01-01
        • 1970-01-01
        • 2016-02-23
        相关资源
        最近更新 更多