Ray Tso
Ray Tso

Reputation: 566

titleLabel.text vs currentTitle in Swift

I am trying to make a simple calculator with Swift. I want to get the "text" on the buttons I created. The instructor in the tutorial is using a property:

@IBAction func appendDigit(sender: UIButton) {
    let digit = sender.currentTitle
}

The question is, if I did this:

@IBAction func appendDigit(sender: UIButton) {
    let digit = sender.titleLabel.text
}

What's the difference? Will they yield the same results? If so, how does one know when to use which?

Upvotes: 4

Views: 7534

Answers (4)

noveleven
noveleven

Reputation: 637

let btn: UIButton = UIButton()
btn.setTitle("123", for: .normal) //Title visible in UI
print(btn.titleLabel?.text) //Optional("123") 
print(btn.currentTitle) //Optional("123")
btn.setTitle("", for: .normal) //Title invisible in UI
print(btn.titleLabel?.text) //Optional("123")
print(btn.currentTitle) //Optional("")

If you want to get the correct title for operations such as copying in UITableViewCell, it's best to use currentTitle!

Upvotes: 0

Dharmesh Kheni
Dharmesh Kheni

Reputation: 71854

For sender.currentTitle:

The current title that is displayed on the button. (read-only)

Declaration

var currentTitle: String? { get }

Discussion

The value for this property is set automatically whenever the button state changes. For states that do not have a custom title string associated with them, this method returns the title that is currently displayed, which is typically the one associated with the UIControlStateNormal state. The value may be nil.

For sender.titleLabel.text:

A view that displays the value of the currentTitle property for a button. (read-only)

Declaration

var titleLabel: UILabel? { get }

Discussion

Although this property is read-only, its own properties are read/write. Use these properties primarily to configure the text of the button. For example:

let button = UIButton.buttonWithType(.System) as UIButton
button.titleLabel.font = UIFont.systemFontOfSize(12)
button.titleLabel.lineBreakMode = .ByTruncatingTail

Do not use the label object to set the text color or the shadow color. Instead, use the setTitleColor:forState: and setTitleShadowColor:forState: methods of this class to make those changes.

The titleLabel property returns a value even if the button has not been displayed yet. The value of the property is nil for system buttons.

From Apple Documentation.

In simple word:

sender.titleLabel?.text = "NewTitle"  //this will work
sender.currentTitle = "newTitle"      //this will give you an error because it is read only property.

And:

let digit = sender.currentTitle
let digit = sender.titleLabel!.text

This both will work because you are reading button's property and assigning it to digit instance.

Upvotes: 0

Shamsudheen TK
Shamsudheen TK

Reputation: 31311

titleLabel.text is mainly using to configure the text of the button(for each state)

currentTitle is read only. This is mainly using to get the title that is currently displayed.

You can't set this property because this set automatically whenever the button state changes. You can use currentTitle to get the title string associated with the button instead of using titleLabel.text because currentTitle property is set automatically whenever the button state changes.

Upvotes: 2

Both .currentTitle and titleLabel.text return same value, but .currentTitle is read-only, you can't modify his value (and it's only available in iOS 8+). .titleLabel is available since iOS 3+ and you can modify text, font, etc.

Upvotes: 0

Related Questions