访问动态构建的按钮

时间:2012-01-04 03:29:12

标签: xcode dynamic properties uibutton

我正在重用一些动态构建UIButtons的代码(没有界面构建器)。我正在使用addTarget:action:forControlEvents方法在每次按下按钮时执行操作。我根据setSelected和isSelected属性更改了按钮的背景颜色。

我添加了一个重置​​按钮,以便一次取消选择所有按钮。但是,我很难确定如何访问动态添加按钮的属性。

创建按钮的代码如下:

for(int y = 1; y < 10; y++)
{

    for(int x = 1; x < 5; x++){
        z++;

        aButton = [UIButton buttonWithType:UIButtonTypeCustom];
        aButton.frame = CGRectMake(x*x_plot, y_plot, 60, 40);
        [aButton setBackgroundImage:[UIImage imageNamed:@"btnUnselected.png"] forState:UIControlStateNormal];
        [aButton addTarget:self action:@selector(digitClick:) forControlEvents:UIControlEventTouchUpInside];
        [aButton setTitle:[NSString stringWithFormat:@"%d",z] forState:UIControlStateNormal];
        aButton.titleLabel.textColor = [UIColor blackColor]; 
        aButton.tag = z;
        [self.view addSubview:aButton];
    }
    y_plot=y_plot+45; //make a 4x9 grid of buttons
}

1 个答案:

答案 0 :(得分:0)

您可以使用此类代码浏览所有子视图

NSArray *ar = self.view.subviews;
[ar enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isKindOfClass:[UIButton class]])
    {
        NSLog(@"I've got 'obj' that is a UIButton on the screen!!!!");
    }
}];