UIButton title changes to default

后端 未结 2 812
半阙折子戏
半阙折子戏 2021-01-07 16:22

I feel like this is probably a stupid question... but anyway I have this kind of weird UIButton title behavior.

The button is set up and connected to bo

相关标签:
2条回答
  • 2021-01-07 16:44

    The swift version of this is

    startButton.setTitle("Start", forState: UIControlState.Normal)
    
    0 讨论(0)
  • 2021-01-07 16:56

    You need to use setTitle:forState: method instead of setting the titleLabel.text property:

    [startButton setTitle:@"Start" forState:UIControlStateNormal];
    // Normal and highlighted titles do not need to be the same
    [startButton setTitle:@"Start!" forState:UIControlStateHighlighted];
    

    What happens now is that you set the title in the label that represents the view of the current state, but once the state changes from pushed to normal, the button resets the label back to the title for the new state (which is the text that you set in the IB).

    0 讨论(0)
提交回复
热议问题