How to get the current Title of a button in Swift 3.0 , ios using sender.titleForState(.Normal)!
Asked Answered
T

12

24

I tried to get the title of a button in swift like below.

@IBAction func buttonAction(_ sender: Any) {
  let buttonTitle = sender.titleForState(.Normal)!
}

but it didn't work,even it doesn't give any hint when we press . after the sender.

so what is the correct way of doing this in swift 3.0

Or else if we create an IBOutlet and then we use its currentTitle, it works fine like below. Why we cannot get it with sender. for above

@IBOutlet var thebutton: UIButton!

@IBAction func buttonAction(_ sender: Any) {
  let buttonTitle = thebutton.currentTitle!
  print(buttonTitle)
}
Transfinite answered 16/10, 2016 at 18:16 Comment(1)
Change sender to UIButton.Mendelson
N
47

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.

Nubianubian answered 16/10, 2016 at 18:20 Comment(5)
yep this is working, but you have to unwrap it like below let buttonTitle = sender.title(for: .normal)!. thanx for the help.Transfinite
just to know I'm asking, why should we check if button title has a value(for nil), we know it has a value, so we can simply unwrap instead of checking nil.Transfinite
Yes, if you are sure it isn't nil then use ! instead of if let clause. I personally avoid using ! as much as possible as it makes your code vulnerable to unexpected exceptions like the ones caused by unwrapping a nil optional.Nubianubian
thanx in advance :)Transfinite
This is working but if in the meantime you have programmatically changed the title of the button, you get the original title and not the current title. To recover the current title you should do : let buttonActualTitle = myButton.titleLabel?.textInheritance
P
11

Smartest version using Swift 5.0:

@IBAction func buttonAction(_ sender: UIButton) {
    print(sender.currentTitle)
}
Polo answered 29/1, 2020 at 20:46 Comment(1)
Here I am getting "Cannot assign to immutable expression of type '@lvalue String??'"Jerald
I
6

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.

Ingressive answered 27/3, 2018 at 2:9 Comment(0)
R
3

In order to resolve the warning, following code may be used.

@IBAction func buttonAction(_ sender: UIButton) {
      let buttonTitle = sender.title(for: .normal) ?? String()
      print(buttonTitle)
    }
Royster answered 5/4, 2020 at 11:51 Comment(0)
T
2

Using Swift version 5.5.2, the best way I found out to print a label's title is:

@IBAction func keyPressed(_ sender: UIButton) {
    print(sender.title(for: .normal)!)
}

I was receiving a warning while using sender.title(for: .normal). The exclamation mark (!) solved it.

Turbulent answered 15/1, 2022 at 20:57 Comment(0)
I
1

The simple way just to use currentTitle:

@IBAction func hardlessSelected(_ sender: UIButton) {

    print(sender.currentTitle)
}
Igbo answered 28/11, 2022 at 22:54 Comment(0)
A
0
@IBAction func buttonPressed(_ sender: AnyObject) {
  let title = sender.title(for: .normal)
  print("\(title) button pressed")
}

So this is for swift 4.0, not the last one.

Autotransformer answered 7/1, 2019 at 19:20 Comment(0)
I
0

If your button has a regular title you can use:

button.title(for: state) // probably UIControl.State.normal

If your UIButton has an attributed title you'll need to use:

self.attributedTitle(for: state)

Or you can use a more general extension:

extension UIButton {
    func titleForState(_ state: UIControl.State = .normal) -> String? {
        if let title = self.title(for: state) {
            return title
        }

        if let attribTitle = self.attributedTitle(for: state) {
            return attribTitle.string
        }

        return nil
    }
}
Insensible answered 25/11, 2019 at 19:48 Comment(0)
I
0

I used switch statment:

var buttonConter = 1
@IBAction func hardlessSelected(_ sender: UIButton) {

    switch buttonConter {
    case 1:
        buttonConter += 1
        print("Soft")
    case 2:
        buttonConter += 1
        print("Medium")
    case 3:
        buttonConter += 1
        print("Hard")
    default:
        print("Non")
    }

    
}

}

Igbo answered 28/11, 2022 at 22:48 Comment(0)
S
0

Instead of using the currentTitle of the UIButton to get the name, you can use titleLabel.

The easy way to get this done is to unwrap the titleLabel then unwrap the text:

@IBAction func buttonAction(_ sender: UIButton) {
    var buttonTitle = sender.titleLabel!.text!
    print(buttonTitle)
}
Sundial answered 15/2, 2023 at 21:41 Comment(0)
M
-1

Since Swift 3.0, it changed from

sender.titleForState(.Normal)! >> sender.title(for : .normal)!

Change Any to AnyObject.

@IBAction func buttonPressed(_ sender: AnyObject) {
  let title = sender.title(for: .normal)!
  print("\(title) button pressed")
}
Marianelamariani answered 13/1, 2018 at 21:51 Comment(1)
AnyObject doesn't have a method title(for:).Nubianubian
D
-4
  @IBAction func btnNextTapped(_ sender: Any) {
    let title = (sender as AnyObject).title(for: .normal)!
  }

Swift4.0

Darren answered 22/5, 2018 at 11:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.