I'm trying to get a button's text to change font color to red when tapped. I looked at similar posts from months ago and use of that code causes a build error in Xcode 6.1.1. Here is the code I'm trying:
我试图获取一个按钮的文本,在点击时将字体颜色更改为红色。我查看了几个月前的类似帖子,使用该代码会导致Xcode 6.1.1中出现构建错误。这是我正在尝试的代码:
class ViewController: UIViewController {
@IBAction func firstButton(sender: UIButton) {
firstButton.titleLabel.textColor = UIColor.redColor()
}
}
The error code that I get is:
我得到的错误代码是:
'(UIButton) -> ()' does not have a member named 'titleLabel'
'(UIButton) - >()'没有名为'titleLabel'的成员
Any help will be much appreciated as I see Swift as my saving grace after having lost patience in trying to learn Objective C.
任何帮助将非常感激,因为我在尝试学习Objective C后失去耐心,因此我认为Swift是我的拯救恩典。
4 个解决方案
#1
8
You're trying to change the text color of the function's titleLabel
, which doesn't make sense. You should be accessing sender
parameter instead if you're trying to get a reference to the button to get at its titleLabel
. Additionally, as rakeshbs points out, titleLabel
is an optional property of UIButton.
您正在尝试更改函数titleLabel的文本颜色,这没有意义。如果您尝试获取对按钮的引用以获取其titleLabel,则应该访问sender参数。另外,正如rakeshbs指出的那样,titleLabel是UIButton的可选属性。
class ViewController: UIViewController {
@IBAction func firstButton(sender: UIButton) {
sender.titleLabel?.textColor = UIColor.redColor()
}
}
If you break down your error message, you'll realize that this is clearly the issue.
如果你打破错误信息,你会发现这显然是个问题。
'(UIButton) -> ()' does not have a member named 'titleLabel'
'(UIButton) - >()'没有名为'titleLabel'的成员
Which states that you are trying to access a member (or property) named titleLabel
on an object of type (UIButton) -> ()
, which means a function that takes a button as input and returns nothing.
这表明您试图在类型(UIButton) - >()的对象*问名为titleLabel的成员(或属性),这意味着一个函数将按钮作为输入并且不返回任何内容。
#2
14
For anyone interested in the exact swift code necessary to make this question of mine work, here it is:
对于任何对使我的工作问题所需的确切快速代码感兴趣的人,这里是:
class ViewController: UIViewController {
@IBAction func firstButton(sender: UIButton) {
sender.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
}
#3
5
this will work for Swift 3 :
这适用于Swift 3:
yourButton.setTitleColor(UIColor.blue, for: .normal)
yourButton.setTitleColor(UIColor.blue,for:。normal)
#4
2
UIButton.titlelabel
is an optional property. You have to use optional chaining for changing its properties.
UIButton.titlelabel是一个可选属性。您必须使用可选链接来更改其属性。
firstButton.titleLabel?.backgroundColor = UIColor.redColor()
Please read about swift optionals to understand this in detail. http://www.appcoda.com/beginners-guide-optionals-swift/
请阅读有关swift选项的详细信息。 http://www.appcoda.com/beginners-guide-optionals-swift/
#1
8
You're trying to change the text color of the function's titleLabel
, which doesn't make sense. You should be accessing sender
parameter instead if you're trying to get a reference to the button to get at its titleLabel
. Additionally, as rakeshbs points out, titleLabel
is an optional property of UIButton.
您正在尝试更改函数titleLabel的文本颜色,这没有意义。如果您尝试获取对按钮的引用以获取其titleLabel,则应该访问sender参数。另外,正如rakeshbs指出的那样,titleLabel是UIButton的可选属性。
class ViewController: UIViewController {
@IBAction func firstButton(sender: UIButton) {
sender.titleLabel?.textColor = UIColor.redColor()
}
}
If you break down your error message, you'll realize that this is clearly the issue.
如果你打破错误信息,你会发现这显然是个问题。
'(UIButton) -> ()' does not have a member named 'titleLabel'
'(UIButton) - >()'没有名为'titleLabel'的成员
Which states that you are trying to access a member (or property) named titleLabel
on an object of type (UIButton) -> ()
, which means a function that takes a button as input and returns nothing.
这表明您试图在类型(UIButton) - >()的对象*问名为titleLabel的成员(或属性),这意味着一个函数将按钮作为输入并且不返回任何内容。
#2
14
For anyone interested in the exact swift code necessary to make this question of mine work, here it is:
对于任何对使我的工作问题所需的确切快速代码感兴趣的人,这里是:
class ViewController: UIViewController {
@IBAction func firstButton(sender: UIButton) {
sender.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
}
#3
5
this will work for Swift 3 :
这适用于Swift 3:
yourButton.setTitleColor(UIColor.blue, for: .normal)
yourButton.setTitleColor(UIColor.blue,for:。normal)
#4
2
UIButton.titlelabel
is an optional property. You have to use optional chaining for changing its properties.
UIButton.titlelabel是一个可选属性。您必须使用可选链接来更改其属性。
firstButton.titleLabel?.backgroundColor = UIColor.redColor()
Please read about swift optionals to understand this in detail. http://www.appcoda.com/beginners-guide-optionals-swift/
请阅读有关swift选项的详细信息。 http://www.appcoda.com/beginners-guide-optionals-swift/