我有一系列按钮,在程序开头我希望用户选择一个按钮然后它将改变它的背景。
我写了一个switch语句,但我不知道如何将它实现到override func viewDidLoad()
,即我不知道应该写什么作为参数而不是UIButton
class ViewController: UIViewController {
@IBOutlet var cardsArray: Array<UIButton> = []
override func viewDidLoad() {
super.viewDidLoad()
self.playerMove(sender: UIButton)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func playerMove(sender: UIButton) {
switch (sender) {
case self.cardsArray[0]:
self.cardPressedAll(0)
case self.cardsArray[1]:
self.cardPressedAll(1)
case self.cardsArray[2]:
self.cardPressedAll(2)
default: break
}
}
func cardPressedAll (cardNumber: Int) {
self.cardsArray[cardNumber].enabled = false
self.cardsArray[cardNumber].setBackgroundImage(UIImage(named: "cross"), forState: UIControlState.Normal)
}
}
答案 0 :(得分:0)
您没有在视图中调用'self.playerMove(sender:UIButton)',而是需要使用故事板将按钮连接到IBAction。