iOS 用for循环写了一排button

来源:互联网 发布:mac server如何使用 编辑:程序博客网 时间:2024/06/11 03:35
- (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];
}

是这个意思么。点了变颜色了。
0 0
原创粉丝点击