Xcode 访问动态构建的按钮

Xcode 访问动态构建的按钮,xcode,dynamic,properties,uibutton,Xcode,Dynamic,Properties,Uibutton,我正在重用一些动态构建UIButtons的代码(没有界面生成器)。我使用addTarget:action:forControlEvents方法在每次按下按钮时执行操作。我根据setSelected和isSelected属性更改按钮的背景色 我添加了一个重置按钮,以便一次取消选择所有按钮。但是,我很难确定如何访问动态添加的按钮的属性 创建按钮的代码如下所示: for(int y = 1; y < 10; y++) { for(int x = 1; x < 5; x++){

我正在重用一些动态构建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
}
for(int y=1;y<10;y++)
{
对于(int x=1;x<5;x++){
z++;
aButton=[UIButton按钮类型:UIButtonTypeCustom];
aButton.frame=CGRectMake(x*x_图,y_图,60,40);
[aButton setBackgroundImage:[UIImage ImageName:@“btunselected.png”]用于状态:UIControlStateNormal];
[aButton addTarget:self action:@selector(数字点击:)for ControlEvents:UIControlEventTouchUpInside];
[aButton setTitle:[NSString stringWithFormat:@“%d”,z]用于状态:UIControlStateNormal];
aButton.titleLabel.textColor=[UIColor blackColor];
aButton.tag=z;
[self.view addSubview:aButton];
}
y_plot=y_plot+45;//制作一个4x9的按钮网格
}

您可以使用这样的代码遍历所有子视图

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!!!!");
    }
}];

您已经设置了
标记
,因此您应该能够执行
UIButton*butt=(UIButton*)[self.view view withtag:tag]
?谢谢,迈克!工作得很好!