其实这个很简单,加一个if判断就可以了,但是我记得刚学到UI的时候,做出来来有点小成就感。以下是代码:
#import "ViewController.h"
@interface ViewController ()
{
UIButton*button;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
button = [[UIButton alloc]initWithFrame:CGRectMake(20, 100, 44, 44)];
[self.view addSubview:button];
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
}
-(void)click
{
if (button.backgroundColor == [UIColor redColor]) {
button.backgroundColor = [UIColor greenColor];
}
else if (button.backgroundColor == [UIColor greenColor]){
button.backgroundColor = [UIColor blueColor];
}
else{
button.backgroundColor = [UIColor redColor];
}
}
就这样了 。