【问题标题】:UIButton text not staying changingUIButton 文本不会保持变化
【发布时间】:2015-05-22 05:43:55
【问题描述】:

我正在用 Swift 开发一个简单的 iPhone 应用

我有一个自定义UITableViewCell。在单元格中,我有一个普通的UIButton,文本为“Hello World”。

在我的自定义单元格代码中,我有以下功能:

@IBAction func buttonAction(sender: AnyObject) {

    if likeButton.titleLabel?.text == "Hello World" {

        likeButton.titleLabel?.text = "Hi!"

    }

}

当我点击UIButton 时,UIButton 的文字变为“嗨!”只是一瞬间!然后它很快又变回“Hello World”……有什么办法可以解决这个问题吗?

为什么要这么做?

【问题讨论】:

    标签: ios uitableview swift uibutton


    【解决方案1】:

    使用UIButtons's setTitle: forState: 而不是在按钮的titleLabel 上设置文本。例如-

    初始设置

    likeButton.setTitle("Hello World", forState: UIControlState.Normal)
    

    点击设置-

    @IBAction func buttonAction(sender: AnyObject) {
    
        if likeButton.titleLabel?.text == "Hello World" {
            likeButton.setTitle("Hi!", forState: UIControlState.Normal)
        }
    
    }
    

    【讨论】:

      【解决方案2】:
      likeButton.setTitle("my text here", forState: UIControlState.Normal)
      

      使用此代码

      【讨论】:

        【解决方案3】:

        做这样的事情:

        @IBAction func buttonAction(sender: AnyObject) {

            likeButton.setTitle("Hi!", forState: UIControlState.Normal)
        

        }

        【讨论】:

          【解决方案4】:

          它在这里工作,希望也在那里工作,设置你的默认值 -

          yourButton.setTitle("Hello", forState: UIControlState.Normal)
          

          当你点击你的按钮时设置这个

          @IBAction func buttonAction(sender: AnyObject) {
          
          if yourButton.titleLabel?.text == "Hello " {
              yourButton.setTitle("Max", forState: UIControlState.Normal)
          }
          

          }

          【讨论】:

            猜你喜欢
            • 2014-10-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-06-25
            • 2011-09-12
            • 2011-01-06
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多