jam*_*mil 1 iphone objective-c background-image uibutton ios
我在我的应用程序中使用了20个UIButton.我在click事件中设置了所有这些UIButton的背景图像.所有这些UIButton都保存在NSMutableArray中.这是代码.
saveBtn = [[NSMutableArray alloc] init];
for (int i=0; i<20; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(spacex, spacey, 30.0, 30.0);
idx = arc4random()%[arr count];
NSString* titre1 = [arr objectAtIndex:idx];
[btn setTitle:titre1 forState:UIControlStateNormal];
spacex = spacex + 30;
[saveBtn addObject:btn];
[self.view addSubview:btn];
}
Run Code Online (Sandbox Code Playgroud)
我在这里成功是我的代码.
UIButton *currentButton = (UIButton *)sender;
UIImage * imgNormal = [UIImage imageNamed:@"subtabButton.png"];
[currentButton setBackgroundImage:imgNormal forState:UIControlStateNormal];
[currentButton setTitle:currentButton.titleLabel.text forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)
但是在这20个UIButton之间有3个UIButton,我希望当玩家点击这三个UIButton中的一个时,所有之前设置的背景图像都会从UIButtons中移除.任何人都可以指导我们如何做到这一点.提前做好准备.
for (UIButton *btn in yourArrayOfButtons)
{
[btn setBackgroundImage:[UIImage imageNamed:@"nameofmyimage"] forState:UIControlStateNormal];
}
Run Code Online (Sandbox Code Playgroud)
或者如果你想删除图像:
for (UIButton *btn in yourArrayOfButtons)
{
[btn setBackgroundImage:nil forState:UIControlStateNormal];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1221 次 |
| 最近记录: |