【问题标题】:How can I access an UIButton in the ViewController Class from a function parameter如何从函数参数访问 ViewController 类中的 UIButton
【发布时间】:2021-02-19 01:40:31
【问题描述】:
如何从具有 2 个参数的函数访问 IBOutlet UIButton
class ViewController: UIViewController {
@IBOutlet weak var btnMaxAloud: UIButton!
override func viewDidLoad() {
btnMaxAloud.tintColor = UIColor.white
changeIconColor(btnMaxAloud , red) // this is gets the error has no member
}
func changeIconColor(_ uIButtonName:String , _ color:String){
if varName {
self.uIButtonName.tintColor = UIColor.color
}
}
【问题讨论】:
标签:
swift
function
parameters
viewcontroller
iboutlet
【解决方案1】:
func changeIconColor(uIButtonName: UIButton, color: UIColor) {
uIButtonName.tintColor = color
}
现在在 viewDidLoad 中
changeIconColor(uIButtonName: btnMaxAloud, color: .red)
【解决方案2】:
您的代码错误,特别是 changeIconColor 的参数类型。例如这应该有效:
class ViewController: UIViewController
{
@IBOutlet weak var btnMaxAloud: UIButton!
override func viewDidLoad()
{
btnMaxAloud.tintColor = UIColor.white
changeIconColor(btnMaxAloud, UIColor.red)
}
func changeIconColor(_ button: UIButton, _ color: UIColor)
{
button.tintColor = color
}
}
函数changeIconColor() 现在接受UIButton 和UIColor 作为参数。