How to dynamically change UIButton title during Touch and Hold

I am following this answer to change the title of my UIButton. To summarize, here's what I need to do:

  • When the user taps down, the button title starts to change.

  • While the touch is being held, it should change every 1 second and the user should be able to see it.

  • When the user touches (stops touching), the button caption should be fixed to the last held value.

I handle 1 using setTitle: forState:

I was treated with 2 using setTitle: forState: . This continues as long as the timer continues to fire after touching the user every 1 second.

I am handling 3 using titleForState . However, here I am not getting the value set to 2. Sometimes when I get the correct value, there is another problem: after touch mode is over, I still see the same old title in this custom UIButton in the UI, not the same which I am reading using titleForState. (in this respect even UIButton.textLabel.text gives the wrong value)

1 - Am I following the correct touch and hold approach? (I mean using the timer approach pictured for setting and reading UIButton titles)

2 - If yes, what should I change in my code to read the correct value of the UIButton title?

EDIT:

I got rid of the problem I got in 3 above. The reason for this question was this: I did not supply the same combination UIControlState

during read and write. I assumed that if you write UIControlStateNormal | UIControlStateHighlighted

to write the title and then use UIControlStateNormal

to read it, it should return currect title and vice versa. Unfortunately, this is not the case. I changed my application logic so that I only need to use one of these states both times.

However, the main problem still remains (2 above) - how to show the title while the UIButton is still in the selected state. The name is completely invisible as long as the touch persists.

+3


source to share


1 answer


how to show title while UIButton is still in selected state. The name is completely invisible as long as the touch persists.

Is your button configured so that the title disappears during a touch event, without changing the title text code? That is, without changing the code, the header still disappears?



I think your button should be redrawn, try calling [button setNeedsDisplay]

after changing the title text.

+1


source







All Articles