I am new in Swift
and I am making a simple calculator where I wan't to detect a button which was pressed and my code is below.
@IBAction func ButtonTapped(TheButton : UIButton){ println(TheButton.titleLabel.text) }
But It Shows me error like "UILabel? Does not have a member a named text"
and it tell me to modify code like this
println(TheButton.titleLabel?.text)
This Prints Optional("1")
(1 is my button name)
So anybody can help me why this is happend to me and how can I print my button name without Optional?
Use the textContent property to get the text of a label element, e.g. const text = label. textContent . The textContent property will return the text content of the label and its descendants. If the element is empty, an empty string is returned.
Yes, your button must have so form of text label associated with it.
If you are sure that titleLabel
is not nil
:
println(TheButton.titleLabel!.text)
else
if let text = TheButton.titleLabel?.text { println(text) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With