I’m having some trouble with sleep(). I have 10 UIButtons which are connected to the same IBAction called “buttons:”. Now, the IBAction method should make all the buttons unenabled and turn the background color of them to red and then call a method called “nextButtonToEnable” which first has a sleep(1) and then a random int thats used by a switch which turns 1 of the buttons enabled and blue instead of red. Now to the problem, I want all the buttons to turn red when pressed and then after 1 sec delay another button will become blue, but that doesn’t happen, what really happens is that when i press the blue button it stays blue during the whole delay of 1 sec and then it turns red and another button is turns blue.
here is my code
.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
IBOutlet UIButton *b1;
IBOutlet UIButton *b2;
IBOutlet UIButton *b3;
IBOutlet UIButton *b4;
IBOutlet UIButton *b5;
IBOutlet UIButton *b6;
IBOutlet UIButton *b7;
IBOutlet UIButton *b8;
IBOutlet UIButton *b9;
IBOutlet UIButton *b10;
}
-(void)nextButtonToEnable;
@end
.m:
-(IBAction)buttons:(id)sender {
b1.enabled = NO;
b2.enabled = NO;
b3.enabled = NO;
b4.enabled = NO;
b5.enabled = NO;
b6.enabled = NO;
b7.enabled = NO;
b8.enabled = NO;
b9.enabled = NO;
b10.enabled = NO;
b1.backgroundColor = [UIColor redColor];
b2.backgroundColor = [UIColor redColor];
b3.backgroundColor = [UIColor redColor];
b4.backgroundColor = [UIColor redColor];
b5.backgroundColor = [UIColor redColor];
b6.backgroundColor = [UIColor redColor];
b7.backgroundColor = [UIColor redColor];
b8.backgroundColor = [UIColor redColor];
b9.backgroundColor = [UIColor redColor];
b10.backgroundColor = [UIColor redColor];
[self nextButtonToEnable];
}
-(void)nextButtonToEnable {
sleep(1);
int nextButton = rand() % 10;
switch (nextButton) {
case 0:
b1.enabled = YES;
b1.backgroundColor = [UIColor blueColor];
break;
case 1:
b2.enabled = YES;
b2.backgroundColor = [UIColor blueColor];
break;
case 2:
b3.enabled = YES;
b3.backgroundColor = [UIColor blueColor];
break;
case 3:
b4.enabled = YES;
b4.backgroundColor = [UIColor blueColor];
break;
case 4:
b5.enabled = YES;
b5.backgroundColor = [UIColor blueColor];
break;
case 5:
b6.enabled = YES;
b6.backgroundColor = [UIColor blueColor];
break;
case 6:
b7.enabled = YES;
b7.backgroundColor = [UIColor blueColor];
break;
case 7:
b8.enabled = YES;
b8.backgroundColor = [UIColor blueColor];
break;
case 8:
b9.enabled = YES;
b9.backgroundColor = [UIColor blueColor];
break;
case 9:
b10.enabled = YES;
b10.backgroundColor = [UIColor blueColor];
break;
default:
break;
}
}
So its like the sleep is between b1.enabled = NO; and b1.backgroundColor = [UIColor redColor];.
How do i fix this because i haven’t found anything on the internet. Mostly because I dont have a single clue what to search for :P.
You should probably be using
performSelector:withObject:afterDelay:and notsleep()