How do I get the UIButton to update?
I have a working snippet line by line:
for (UIButton *b in buttonMapping) {
[b setTitle:@"yo!" forState:UIControlStateNormal];
[NSThread sleepForTimeInterval:1.0];
}
There are four buttons, all four buttons are being updated. However, instead of refreshing one second, four seconds pass and they are all refreshed.
How can I get the UIButton to update? Or is this not the recommended sleep method?
source to share
[b setNeedsDisplay];
I also don't recommend sleeping on the main thread (like you are here) as this disables all user interactions.
There are several alternatives. Can be used NSTimer
to execute a specific method once per second. However, the easiest way would be to do something like:
for (NSUInteger idx = 0; idx < [buttonMapping count]; idx++) {
UIButton * b = [buttonMapping objectAtIndex:idx];
[b performSelector:@selector(setNormalStateTitle:) withObject:@"yo!" afterDelay:(idx*60)];
}
Then add a method to the UIButton (i.e. a category) called setNormalStateTitle:
that just executes the method setTitle:forControlState:
. With this approach, you don't need a method setNeedsDisplay
.
source to share