Ios UIButton文本未保持更改状态

Ios UIButton文本未保持更改状态,ios,uitableview,swift,uibutton,Ios,Uitableview,Swift,Uibutton,我正在用Swift开发一个简单的iPhone应用程序 我有一个自定义的UITableViewCell。在单元格中,我有一个正常的ui按钮,文本为“Hello World” 在自定义单元格的代码中,我具有以下功能: @IBAction func buttonAction(sender: AnyObject) { if likeButton.titleLabel?.text == "Hello World" { likeButton.titleLabel?.text =

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

我有一个自定义的
UITableViewCell
。在单元格中,我有一个正常的
ui按钮
,文本为“Hello World”

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

@IBAction func buttonAction(sender: AnyObject) {

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

        likeButton.titleLabel?.text = "Hi!"

    }

}
当我点击
ui按钮
时,
ui按钮
的文本会在瞬间变为“嗨!”!然后它会很快变回“Hello World”…有没有办法解决这个问题

它为什么这样做

likeButton.setTitle("my text here", forState: UIControlState.Normal)
使用此代码


使用此代码,而不是在按钮的标题标签上设置文本。比如说-

初始设置

likeButton.setTitle("Hello World", forState: UIControlState.Normal)
单击“设置”-

@IBAction func buttonAction(sender: AnyObject) {

    if likeButton.titleLabel?.text == "Hello World" {
        likeButton.setTitle("Hi!", forState: UIControlState.Normal)
    }

}

使用
ui按钮的
setTitle:forState:
而不是在按钮的
标题标签
上设置文本。比如说-

初始设置

likeButton.setTitle("Hello World", forState: UIControlState.Normal)
单击“设置”-

@IBAction func buttonAction(sender: AnyObject) {

    if likeButton.titleLabel?.text == "Hello World" {
        likeButton.setTitle("Hi!", forState: UIControlState.Normal)
    }

}

这样做:

@iAction func按钮操作(发件人:AnyObject){


}

执行以下操作:

@iAction func按钮操作(发件人:AnyObject){


}

它在这里起作用,希望在那里也起作用,设置默认值-

yourButton.setTitle("Hello", forState: UIControlState.Normal)
当你点击按钮时,设置这个

@IBAction func buttonAction(sender: AnyObject) {

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

}

它在这里起作用,希望在那里也起作用,设置默认值-

yourButton.setTitle("Hello", forState: UIControlState.Normal)
当你点击按钮时,设置这个

@IBAction func buttonAction(sender: AnyObject) {

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