How to get the current Title of a button in Swift 3.0 , ios using sender.titleForState(.Normal)!
Because parameter sender is in type Any
instead of UIButton
. Change the method signature to:
@IBAction func buttonAction(_ sender: UIButton) {
if let buttonTitle = sender.title(for: .normal) {
print(buttonTitle)
}
}
and you should be good to go.
To get the title of the button regardless of its current state in swift 3.0 try using this:
@IBAction func buttonPressed(_ sender:UIButton){
let buttonTitle = sender.titleLabel?.text
print("\(String(describing: buttonTitle)")
}
This will return the title for the state, based on the state that the button is current in.
Smartest version using Swift 5.0:
@IBAction func buttonAction(_ sender: UIButton) {
print(sender.currentTitle)
}