- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
float x = 20;
float y = 100;
float w = 30;
float h = 30;
for (int i = 0; i<5; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setFrame:CGRectMake(w*i+x, y, w, h)];
btn.backgroundColor = [UIColor redColor];
[btn setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btn_click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
}
- (void)btn_click:(id)sender
{
UIButton *btn = sender;
btn.backgroundColor = [UIColor greenColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button = [[UIButton alloc] initWithFrame:CGRectMake(i * 60, 60, 20, 20)];
[button addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
float x = 20;
float y = 100;
float w = 30;
float h = 30;
for (int i = 0; i<5; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setFrame:CGRectMake(w*i+x, y, w, h)];
btn.backgroundColor = [UIColor redColor];
[btn setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btn_click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
}
- (void)btn_click:(id)sender
{
UIButton *btn = sender;
btn.backgroundColor = [UIColor greenColor];
}
for (int i = 0; i < 3; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button = [[UIButton alloc] initWithFrame:CGRectMake(i * 60, 60, 20, 20)];
[button addTarget:self action:@selector(changeColor:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}