【问题标题】: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() 现在接受UIButtonUIColor 作为参数。

      【讨论】:

        猜你喜欢
        • 2020-03-05
        • 1970-01-01
        • 2020-07-11
        • 1970-01-01
        • 2018-06-23
        • 2022-11-09
        • 2020-07-14
        • 2020-06-17
        • 1970-01-01
        相关资源
        最近更新 更多