gpt4 book ai didi

ios - 如何使用 switch 语句更改 UIButton 的图像?

转载 作者:行者123 更新时间:2023-11-28 21:53:16 25 4
gpt4 key购买 nike

我能够向数组中的 UIButton 添加“setbackgroundimage”。现在所有按钮都具有相同的图像。如何创建 switch 语句以便能够为我的 NSarray 中的每个 initwithobjects @""设置不同的图像?

下面是我使用的代码:

 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);
}

最佳答案

设置选中图片:

[mybutton setBackgroundImage:[UIImage imageNamed:@"YOUR_SELECTED_IMG.png"] forState:UIControlStateSelected];

虽然你想设置不同的每个,你可以链接 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;
}

关于ios - 如何使用 switch 语句更改 UIButton 的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27477023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com