我有几个从 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];
}
我还希望为每个按钮调用不同的选择器。