ios - 如何在我的代码中重用/导入局部变量到另一个地方?

标签 ios xcode swift

我正在尝试在 Xcode 中使用 swift 制作井字游戏。

我有一个按钮常量,需要插入九次才能制作井字棋盘。当我的初始化程序中有常量时,板会按预期创建,但是当按下其中一个按钮时,我无法在我的函数中访问按钮的属性。

如果我尝试在初始化程序之外创建常量,我可以在我的函数中调用它,但它只会在面板的最后一个图 block (右下角)上创建一个按钮。

另一方面,我试图在按钮之间留出一些间距,但它只会移动整个板而不是在按钮之间留出间距,所以如果你能帮我解决这个问题,那就太好了。

    import UIKit

class TicTacToeClass: UIView {


    var turn = 0 //0 = X's turn, 1 = O's turn
    var tileType = 0 //0 = empty, 1 = X, 2 = O
    var row = 0 //0 = row1, 1 = row2, 2 = row3
    var column = 0
    let spacing = 5
    let tiles = 9 //number of tiles
    let emptyTile = UIImage(named: "emptyTile")


    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)


        for _ in 0..<tiles {
            let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

            button.frame = CGRect(x: 0+column*100+spacing, y: 0+row*100+spacing, width: 100, height: 100)
            button.backgroundColor = UIColor.blackColor()
            button.setImage(emptyTile, forState: .Normal)
            button.setImage(emptyTile, forState: .Highlighted)
            button.setImage(emptyTile, forState: [.Normal, .Highlighted])
            button.adjustsImageWhenHighlighted = false
            button.addTarget(self, action: "setTile", forControlEvents: .TouchDown)
            print("row: \(row), column: \(column)")

            addSubview(button)

            if row == 0 {
                if column == 0 || column == 1{
                    column++
                }
                else if column == 2 {
                    row++
                    column = 0
                }
            }
            else if row == 1 {
                if column == 0 || column == 1{
                    column++
                }
                else if column == 2 {
                    row++
                    column = 0
                }

            }
            else if row == 2 {
                if column == 0 || column == 1{
                    column++
                }
                else if column == 2 {
                    row++
                }

            }
            else {
                print("****")
            }
        }

    }

    func setTile() {
        if turn == 0 {
            print("test X")
            //button.backgroundColor = UIColor.redColor()


            turn = 1
        }
        else if turn == 1 {
            print("test O")
            //button.backgroundColor = UIColor.blueColor()

            turn = 0
        }
        else {
            print("****")
        }
    }
}

最佳答案

您可以在初始化器中创建按钮时访问该按钮,只需更改如下内容即可:

// button.addTarget(self, action: "setTile", forControlEvents: .TouchDown)
button.addTarget(self, action: "setTile:", forControlEvents: .TouchDown)

然后

        //func setTile() {
        func setTile(sender:UIButton!) {
            // sender is the pressed button. You can do everything with it.
            if turn == 0 {
                print("test X")
                //button.backgroundColor = UIColor.redColor()
                sender.backgroundColor = UIColor.redColor()

                turn = 1
            }
            else if turn == 1 {
                print("test O")
                //button.backgroundColor = UIColor.blueColor()
                sender.backgroundColor = UIColor.blueColor()
                turn = 0
            }
            else {
                print("****")
            }
        }

关于ios - 如何在我的代码中重用/导入局部变量到另一个地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33666018/

相关文章:

ios - 在 Xcode Swift 中显示空白/白色 View 的弹出窗口演示文稿

ios - 我可以从 iOS 应用程序分享多个图像和多个视频吗

ios - 在 popover segues 中 Anchor 和 Passthrough 有什么用?

c - OpenGL 纹理球体使用土壤?

swift - Alamofire https 请求仅在 NSExceptionAllowsInsecureHTTPLoads 设置为 true 时有效

ios - 测试iOS应用程序的安全性

ios - 发布时将应用限制在 iPhone 上

swift - 不检测碰撞

ios - Flurry Analytics 无法显示点击事件

ios - 我在使用 JSON Decoder 时做错了什么