[英]How to change image for UIButton using switch statement?
I was able to add a "setbackgroundimage" to my UIButton in my array. 我能够在数组中的UIButton中添加“ setbackgroundimage”。 Now all buttons have the same image. 现在,所有按钮都具有相同的图像。 How do I create a switch statement to be able to set a different image for each initwithobjects @"" in my NSarray? 如何创建一个switch语句,以便能够为NSarray中的每个initwithobjects @“”设置不同的图像?
Below is the code I'm using: 以下是我正在使用的代码:
menuItems = [[NSArray alloc]initWithObjects:@"",@"",@"",@"",@"",@"",@"", nil];
for (int b=0;b<[menuItems count];b++) {
UIButton *mybutton = [[UIButton alloc]initWithFrame:CGRectMake(3.0f, originofButtons, buttonWidth, buttonHeight)];
//mybutton.backgroundColor = [UIColor redColor];
[mybutton setBackgroundImage:[UIImage imageNamed:@"menubutton.png"]
forState:UIControlStateNormal];
[mybutton setTag:b];
[mybutton setTitle:[menuItems objectAtIndex:b] forState:UIControlStateNormal];
[mybutton setSelected:false];
[mybutton addTarget:self action:@selector(buttonpress:) forControlEvents:UIControlEventTouchUpInside];
[m_scrollview addSubview:mybutton];
originofButtons += (buttonHeight + buttonseparator);
}
To set the image for selected: 设置所选图像:
[mybutton setBackgroundImage:[UIImage imageNamed:@"YOUR_SELECTED_IMG.png"] forState:UIControlStateSelected];
Whereas you want to set different each, you can link tag
. 尽管您想分别设置不同的名称,但是可以链接tag
。
switch (myButton.tag) {
case 0:
[mybutton setBackgroundImage:[UIImage imageNamed:@"YOUR_SELECTED_IMG.png"] forState:UIControlStateSelected];
break;
case 1:
[mybutton setBackgroundImage:[UIImage imageNamed:@"YOUR_SELECTED_IMG.png"] forState:UIControlStateSelected];
break;
}
This can be help you for your scenario. 这可以为您的方案提供帮助。 You can add your button image name in NSArray and then set image for each case using the below code snippet: 您可以在NSArray中添加按钮图像名称,然后使用以下代码段为每种情况设置图像:
NSArray* menuItems = [[NSArray alloc]initWithObjects:@"FirstScreen",@"SecondScreen",@"ThridScreen", nil];
for (int b=0;b<[menuItems count];b++) {
UIButton *mybutton = [[UIButton alloc]init ];//WithFrame:CGRectMake(3.0f, originofButtons, buttonWidth, buttonHeight)];
[mybutton setBackgroundImage:[UIImage imageNamed:[menuItems[b] stringByAppendingString:@".png"]] forState:UIControlStateSelected];
i am hoping this will be helpful to you according to your requirements..... 我希望这会根据您的要求对您有所帮助.....
menuItems = [[NSArray alloc]initWithObjects:@"",@"",@"",@"",@"",@"",@"", nil];
for (int b=0;b<[menuItems count];b++) {
UIButton *mybutton = [[UIButton alloc]initWithFrame:CGRectMake(3.0f, originofButtons, buttonWidth, buttonHeight)];
//mybutton.backgroundColor = [UIColor redColor];
[mybutton setBackgroundImage:[self getImageFromIndex:b %4]
forState:UIControlStateNormal];
[mybutton setTag:b];
[mybutton setTitle:[menuItems objectAtIndex:b] forState:UIControlStateNormal];
[mybutton setSelected:false];
[mybutton addTarget:self action:@selector(buttonpress:) forControlEvents:UIControlEventTouchUpInside];
[m_scrollview addSubview:mybutton];
originofButtons += (buttonHeight + buttonseparator);
}
-(UIImage *) getImageFromIndex:(short) index
{
switch (index) {
case 0:
return [UIImage imageNamed:@"card_01.png"];
break;
case 1:
return [UIImage imageNamed:@"card_02.png"];
break;
case 2:
return [UIImage imageNamed:@"card_03.png"];
break;
case 3:
return [UIImage imageNamed:@"card_04.png"];
break;
default:
return [UIImage imageNamed:@"card_01.png"];
break;
}
return nil;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.