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

时间:2015-11-12 07:11:08

标签: ios xcode swift

我正试图在Xcode中快速制作Tic Tac Toe。

我有一个常量需要插入九次才能制作Tic Tac Toe板。当我在初始化程序中使用常量时,会按原样创建该板,但是当我们按下其中一个时,我无法访问我的函数中的按钮属性。

如果我尝试在初始化器之外创建常量,我可以在我的函数中调用它,但是它只在板的最后一块瓷砖(右下角)上创建一个按钮。

另一方面,我试图在按钮之间留出一些间距,但是它只会移动整个电路板,而不是在按钮之间间隔开,所以如果你能帮助我,那就太棒了。 / p>

    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("****")
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以在initialiser创建按钮时访问该按钮,只需更改内容:

// 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("****")
            }
        }