【问题标题】:Xcode 8: fix for error (has no member 'backgroundColor') [closed]Xcode 8:修复错误(没有成员'backgroundColor')[关闭]
【发布时间】:2018-08-02 00:05:06
【问题描述】:

只是试图设置按钮的背景颜色,但我在 Xcode 8.3.3 中不断收到以下错误。

'(UIButton) -> ()' 类型的值没有成员'backgroundColor'

来自 xcode 的错误截图

【问题讨论】:

  • 删除按钮声明后的大括号。
  • 你的“sampleButton”是一个动作,而不是一个出口。
  • 首先:请不要发布您的代码截图,请将代码粘贴到您的问题中。关于你的问题:sampleButton 不是UIButton,而是一个处理按钮按下的函数,当然没有背景颜色。您需要一个适当的按钮变量,例如@IBOutlet var sampleButton: UIButton!.
  • 因为(UIButton) -> () 是在内部传递 UIButton 而不是实际的 UIButton...
  • @dr_barto 谢谢!你是对的。我创建了一个插座并应用了背景颜色。关于屏幕截图,我添加了它以显示错误。也应该添加代码。

标签: ios swift xcode8


【解决方案1】:

你应该得到按钮的出口来编辑它的属性。您所拥有的是针对特定事件的该按钮的操作,例如touchUpInside

你应该像这样得到按钮的出口:

@IBOutlet weak var sampleButton: UIButton!

然后在viewDidLoad()中设置backgroundColor

sampleButton.backgroundColor = UIColor.orange

【讨论】:

    猜你喜欢
    • 2012-09-22
    • 2021-03-20
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    相关资源
    最近更新 更多