UIButton文字不会改变

时间:2015-05-22 05:43:56

标签: ios uitableview swift uibutton

我正在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”......有什么方法可以解决这个问题吗?

为什么这样做?

4 个答案:

答案 0 :(得分:3)

使用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)
    }

}

答案 1 :(得分:1)

likeButton.setTitle("my text here", forState: UIControlState.Normal)

使用此代码

答案 2 :(得分:0)

做这样的事情:

@IBAction func buttonAction(sender:AnyObject){

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

}

答案 3 :(得分:0)

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

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

点击按钮设置此

@IBAction func buttonAction(sender: AnyObject) {

if yourButton.titleLabel?.text == "Hello " {
    yourButton.setTitle("Max", forState: UIControlState.Normal)
}

}

相关问题