为什么不能打印一些消息

时间:2015-09-17 14:52:59

标签: ios swift

我使用循环来创建一些按钮,我在循环中添加了Listener,它应该执行“get()”函数并在控制台中输出按钮的标签,但为什么它不起作用?

func setupGameBoard() {
    for i in 0..<2 {
        if i == 1 {
            initialX = CGFloat(horizontalTilePadding)
            initialY = verticalTilePadding!
        }
        else {
            initialX = CGFloat(horizontalTilePadding)
            initialY = CGFloat(verticalTilePadding!)
        }

        var tileX = initialX
        var tileY = initialY

        for j in 0..<dimension {
            tileX = initialX
            for k in 0..<colomn {
               let tile = UIButton(frame: CGRectMake(tileX, tileY,tileWidth,tileWidth))
                tile.setTitle("a", forState: UIControlState.Normal)
                tile.addTarget(self, action: Selector("get:"), forControlEvents: UIControlEvents.TouchUpInside)
                ButtonArray.append(tile)

                tile.backgroundColor = UIColor.blackColor()
                if(i == 0){
                    TopView?.addSubview(tile)
                }
                else {
                    ButtomView?.addSubview(tile)
                }
                tileX += tileWidth+horizontalTilePadding
            }
            tileY += tileWidth+verticalTilePadding!
        }
    }
}

func get(button : UIButton) {
    print(button.titleForState(.Normal), appendNewline: false)
}

1 个答案:

答案 0 :(得分:0)

相当常见的问题,我建议您尝试设置:

TopView?.userInteractionEnabled = false
BottomView?.userInteractionEnabled = false

如果有帮助,请告诉我。