checking UIButton Title in Swift

走远了吗. 提交于 2021-02-16 15:52:33

问题


I have a button and title of the button is an integer.

correctAnswer = randomNumber1 + randomNumber2
button1.setTitle("\(correctAnswer)", forState: UIControlState.Normal)

I am trying to check the title of the button so convert it to an integer and use it in an if statement, but couldn't make it work.

button1.titleLabel?.text?.toInt()
    if button1.titleLabel == correctAnswer { //most probably I am making a mistake here.
        scoring() } else {
        println(button1.titleLabel?.text!)

I am new to swift , I might even have problem with my approach, your help will be appreciated.


回答1:


you should use button1.currentTitle




回答2:


also you should use

 sender.currentTitle



回答3:


you can try using

sender.titleLabel



来源:https://stackoverflow.com/questions/27659766/checking-uibutton-title-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!