[英]SWIFT function with parameters in viewDidLoad
我有一組按鈕,在程序開始時,我希望用戶選擇一個按鈕,然后它將改變其背景。 我已經寫了一個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)
}
}
無需加載視圖中的“ self.playerMove(sender:UIButton)”,而是需要使用情節提要將按鈕連接到IBAction。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.