Home > Software engineering >  How get text from button clicked in swift
How get text from button clicked in swift

Time:08-13

I have a button in storyboard, i wanna when clicked on button get text from sender @IBaction, sample code in below

@IBAction func click(_ sender: UIButton) {
  print(sender.titleLabel?.text)
  print(sender.currentTitle)
}

Now my problem is that when I get the button text from sender.titleLabel?.text it returns the value of optional(button text) and when i try get button text from sender.currentTitle it return the nil.

CodePudding user response:

sender.titleLabel?.text is returning an Optional because titleLabel might be nil.

You need to unwrap the Optional.

  • Related