【问题标题】:How do I reuse/import a local variable to another place in my code?如何在我的代码中重用/导入局部变量到另一个地方?
【发布时间】:2015-11-12 07:11:07
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: ios xcode swift


    【解决方案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("****")
                }
            }
    

    【讨论】:

    • 我看不到第一部分的变化,对吗?另外,当我在 setTile 函数中使用 sender 时,按下任何按钮都会出错。 (线程 1:信号 SIGABRT)
    • @StevoHN "setTile" -> "setTile:"。注意“:”
    • 哦,我有点新。你介意告诉我它有什么不同吗?
    • 现在可以用了吗? "setTile" 表示不向"setTile" 函数传递任何参数,"setTile:" 表示您将传递1,并且是按钮调用addTarget
    • 是的,现在可以使用了,非常感谢。好吧,我想我明白了。最后你能帮我解决我的间距问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多