1

我有几个从 NSMutableArray 创建的 UIButton。我正在尝试使用独特的图像自定义每个 UIButton。

在以下行中,我不确定用于分别为图像数组中的每个按钮设置图像的语法:

[btn setImage:[UIImage imageNamed:[myImages objectAtIndex:3]] forState:UIControlStateNormal];

这是我的其余代码:

NSMutableArray* buttonArray = [[NSMutableArray alloc] init];

for(int i = 0; i < 8; i++)
{
    // Custom UIButton
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    [btn setFrame:CGRectMake(0.0f, 2.0f, 52.0f, 52.0f)];
    [btn setTitle:[NSString stringWithFormat:@"Button %d", i+1] forState:UIControlStateNormal];

    NSArray * myImages = [NSArray arrayWithObjects:@"category0.png", @"category-clothing1.png" , @"category2.png", @"category3.png", nil];

    [btn setImage:[UIImage imageNamed:[myImages objectAtIndex:3]] forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [buttonArray addObject:btn];

}

我还希望为每个按钮调用不同的选择器。

4

2 回答 2

2

像这样的东西?我刚刚看到您关于每个按钮需要不同操作的帖子,但我会检查按钮的发送者并在您的 (buttonPressed:) 方法中相应地调整代码。您还应该确保您的按钮阵列具有适当数量的图像,因此更好的方法可能是仅根据您的图像阵列计数创建按钮数量。(答案已编辑以反映这一点)。我还想补充一点,您应该养成在更改正常状态时也更改图像突出显示状态的习惯,这样当您按下图像时它不会变为空白。

NSMutableArray* buttonArray = [NSMutableArray array];
NSArray * myImages = [NSArray arrayWithObjects:@"category0.png", @"category-clothing1.png" , @"category2.png", @"category3.png", nil];

for(int i = 0;i < [myImages count]; i++)
{
    // Custom UIButton
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

    [btn setFrame:CGRectMake(0.0f, 2.0f, 52.0f, 52.0f)];
    [btn setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:[myImages objectAtIndex:i]] forState:UIControlStateHighlighted];
    [btn setImage:[UIImage imageNamed:[myImages objectAtIndex:i]] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [buttonArray addObject:btn];
}
于 2012-12-05T15:56:54.913 回答
2

此代码包含来自数组的图像和选择器。(此代码未经编译检查,可能存在一些小问题,不便之处敬请见谅)

NSMutableArray* buttonArray = [NSMutableArray array];
NSArray * myImages = [NSArray arrayWithObjects:@"category0.png", @"category-clothing1.png" , @"category2.png", @"category3.png", nil];


// You need to assign string name and that should be your method name.
NSMutableArray *selectors=[[NSMutableArray alloc]initWithArray:@"sel1",@"sel2",@"sel3",@"sel4", nil];


for(int i=0; i<[myImages count]; i++){
    // Custom UIButton
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(0.0f, 2.0f, 52.0f, 52.0f)];
    [btn setTitle:[NSString stringWithFormat:@"Button %d", i] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:[myImages objectAtIndex:i]] forState:UIControlStateHighlighted];
    [btn setImage:[UIImage imageNamed:[myImages objectAtIndex:i]] forState:UIControlStateNormal];

    SEL selector = selectorFromString([selectors objectAtIndex:i]);
    [btn addTarget:self action:@selector(selector) forControlEvents:UIControlEventTouchUpInside];

    [buttonArray addObject:btn];
}
于 2012-12-05T16:06:56.867 回答