Button text reverts when tapped

后端 未结 1 1620
北荒
北荒 2021-01-26 01:47

I have a button that throughout the program he changes his name.
original name is \"line\".
is then renamed to \"bar\".

When is named \"bar\" and I press it

相关标签:
1条回答
  • 2021-01-26 02:18

    The problem is that you are setting _bt3.titleLabel.text directly. Don't do that.

    A button has states: normal, highlighted, selected, and disabled. It knows what its text should be for each state. When _bt3 changes state, it sets _bt3.titleLabel.text. This overrides your change to _bt3.titleLabel.text.

    If you don't set the text for a non-normal state, the button uses the text for the normal state.

    When the user touches _bt3, _bt3 changes its state to highlighted and sets _bt3.titleLabel.text to the text set for its highlighted state. When the user stops touching _bt3, _bt3 changes its state back to normal and sets _bt3.titleLabel.text to the text set for its normal state.

    So instead of setting _bt3.titleLabel.text directly, you need to tell the button what text it should display in the normal state:

    [_bt3 setTitle:@"Bar" forState:UIControlStateNormal];
    
    0 讨论(0)
提交回复
热议问题