我正在尝试将字符串转换为按钮类型。基本上,我循环通过5个按钮,命名为btn1,btn2…btn5。以下是片段:
- (IBAction)replaceImg
{
UIButton* b;
for(int i=0; i<5; i++)
{
b = (UIButton*)[NSString stringWithFormat:@"btn%d",i]; //1!
if([b isHighlighted])
{
int imgNo = (arc4random() % 6) + 1;
UIImage *img = [UIImage imageNamed:[NSStringstringWithFormat:@"%d.png", imgNo]];
[b setImage:img forState:(UIControlState)UIControlStateNormal];
}
}
}
The line marked 1 is giving a problem, if I swap it with b = btn1, it works perfectly. Please help! I couldnt find a way to access a button by its name either. Like UIImage has something like imageNamed.