【问题标题】:checking UIButton Title in Swift在 Swift 中检查 UIButton 标题
【发布时间】:2014-12-26 17:02:58
【问题描述】:

我有一个按钮,按钮的标题是一个整数。

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

我正在尝试检查按钮的标题,以便将其转换为整数并在 if 语句中使用它,但无法使其工作。

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

我是 swift 新手,我什至可能对我的方法有疑问,我们将不胜感激。

【问题讨论】:

    标签: ios swift uibutton xcode6


    【解决方案1】:

    你应该使用button1.currentTitle

    【讨论】:

    • 我试过了,但这给出了一个错误button1.currentTitle == correctAnswer 错误是:类型'字符串?'不符合协议'_RawOptionSetType'
    • 然后试试 button1.currentTitle.toInt()! == 正确答案
    【解决方案2】:

    你也应该使用

     sender.currentTitle
    

    【讨论】:

      【解决方案3】:

      你可以试试

      sender.titleLabel

      【讨论】:

      • 您能提供更多信息吗?展示提问者将如何在他们的代码中使用该调用,而不仅仅是一个调用。
      • 由于这个帖子已经有了公认的答案以及您提供的相同解决方案,如果没有任何新内容,最好不要添加新帖子。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 2016-02-03
      • 1970-01-01
      相关资源
      最近更新 更多