liangdapang
liangdapang

Reputation: 312

IOS/swift currentTitle return nil

When I use titlelabel.text, it can print my results normally, but when I use currenttitle, it returns nil. I checked the document. currenttitle and titlelabel.text are quick access methods, but why do I get different results

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

The screenshot of the problem is as follows

enter image description here

Upvotes: 0

Views: 2054

Answers (3)

Achyuth kumar
Achyuth kumar

Reputation: 1

I'm building a Quizz app and I can confirm changing the button style from Plain to Default solves the problem of sender.currentTitle and sender.titlelabel?.text returning nil/not Working.

After some search i found this.

var res = (sender as AnyObject).currentTitle

Upvotes: 0

devindesu
devindesu

Reputation: 1

I'm also building a calculator app and I can confirm changing the button style from Plain to Default solves the problem of sender.currentTitle returning nil.

Upvotes: 0

liangdapang
liangdapang

Reputation: 312

Thanks everyone, I solved the problem,my button style is pain! When I change it to default it works! The same reason in pain mode,title text position is not working. I changed it to default, now it works!

Upvotes: 7

Related Questions