简体   繁体   English

在viewDidLoad中带有参数的SWIFT函数

[英]SWIFT function with parameters in viewDidLoad

I have an array of buttons and at the beginning of the program I want the user to pick one button and then it'll change its background. 我有一组按钮,在程序开始时,我希望用户选择一个按钮,然后它将改变其背景。 I've written a switch-statement but I don't know how can I implement it into override func viewDidLoad() , namely, I have no idea what should I write as a parameter instead of UIButton 我已经写了一个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.

相关问题 viewDidLoad中的TextView函数(Swift) - function for TextView in viewDidLoad (Swift) Swift 3-ViewDidLoad中的函数无法完全执行 - Swift 3 - function in viewdidload not completly executing 无法快速加载viewDidLoad()中的函数 - Unable to overload function in viewDidLoad() in swift Swift中viewdidload内部的@IB Action函数 - @IB Action function inside of viewdidload in Swift 通过NSTimer在ViewDidLoad中调用函数-Swift - Calling function in ViewDidLoad through NSTimer - swift Swift自定义updateUI()函数不适用于viewDidLoad - Swift custom updateUI() function does not work on viewDidLoad 快速访问在覆盖函数func中创建的变量viewDidLoad(){super.viewDidLoad()function - accessing variables created in the override func viewDidLoad() { super.viewDidLoad() function outside that function in swift 从它的功能中获取placemark.locality到viewDidLoad函数中(Swift) - Getting placemark.locality from it's function into the viewDidLoad function (Swift) 从函数内部访问常量或变量到`viewdidload`中以保存在Firebase Swift 3中 - Access constant or variables from inside a function to in `viewdidload` to save in firebase swift 3 在 swift 中使用函数 viewDidLoad 中的代码时,滚动到底部自动不起作用 - The scroll to bottom auto not work when use the code in function viewDidLoad in swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM