Ios 更改以编程方式创建的已单击UIButton的背景图像
我已经通过编程创建了Ios 更改以编程方式创建的已单击UIButton的背景图像,ios,objective-c,iphone,uibutton,Ios,Objective C,Iphone,Uibutton,我已经通过编程创建了ui按钮,并将它们添加到了我的uicrollview。现在我想将单击的ui按钮的背景图像更改为图像,其他按钮应保持默认颜色的蓝色。现在,当我单击另一个ui按钮,它将显示为蓝色,我想将以前单击的ui按钮更改为蓝色,并将当前单击的按钮更改为bg图像 这是创建和设置ui按钮属性的代码: for (int i=0; i<[arrPartVenuDetails count]; i++) { NSString *venueId = [[arrPartVenu
ui按钮
,并将它们添加到了我的uicrollview
。现在我想将单击的ui按钮
的背景图像更改为图像,其他按钮应保持默认颜色的蓝色。现在,当我单击另一个ui按钮
,它将显示为蓝色,我想将以前单击的ui按钮
更改为蓝色,并将当前单击的按钮更改为bg图像
这是创建和设置ui按钮
属性的代码:
for (int i=0; i<[arrPartVenuDetails count]; i++) {
NSString *venueId = [[arrPartVenuDetails objectAtIndex:i] objectForKey:@"venue_id"];
NSInteger venue_id= [venueId integerValue];
NSLog(@"venue id %ld",(long)venue_id);
aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setFrame:CGRectMake(10.0f, 10.0f, 100.0f, 20.f)];
[aButton setBackgroundColor:[UIColor colorWithRed:(6/255.0) green:(130/255.0) blue:(195/255.0) alpha:1]];
NSString *buttonClassName = [NSString stringWithFormat:@"VENUE %d", i+1];
[aButton.titleLabel setTextAlignment: NSTextAlignmentCenter];
[aButton setTitle:buttonClassName forState:UIControlStateNormal];
aButton.frame = CGRectMake(xCoord, yCoord,buttonWidth,buttonHeight );
[aButton addTarget:self action:@selector(venueButtonClick:) forControlEvents:UIControlEventTouchUpInside];
aButton.tag = venue_id;
[scrollVenue addSubview:aButton];
yCoord += buttonHeight + buffer;
}
修改您的
venuebutton单击,如下所示:
-(void) venueButtonClick:(UIButton *)sender{
for (UIButton *btn in scrollVenue) {
if (btn.tag==sender.tag) {
[btn setBackgroundImage:[UIImage imageNamed:@"venue .png"] forState:UIControlStateNormal];
}
else{
[btn setBackgroundColor:[UIColor colorWithRed:(6/255.0) green:(130/255.0) blue:(195/255.0) alpha:1]];
}
}
}
将新属性添加到.h
文件中
@property NSUInteger lastSelectedTag;
在视图didload
中,为其指定一些安全值:
_lastSelectedTag = 80807652;//example value as it will be unique
现在,在您的iAction
中,按如下方式更改它:
-(void) venueButtonClick:(UIButton *)sender{
if(_lastSelectedTag!=80807652)
{
//lastSelectedTag is different than our safe value so it means that it now contains tag of real last selected button
UIButton *lastSelectedButton = [scrollVenue viewWithTag:_lastSelectedTag]; //This is the previously selected button
//Change it to blue and do whatever you want
[lastSelectedButton setBackgroundImage:nil forState:UIControlStateNormal];//remove background image
[lastSelectedButton setBackgroundColor:[UIColor colorWithRed:(6/255.0) green:(130/255.0) blue:(195/255.0) alpha:1]]; //give it color
}
[sender setBackgroundImage:[UIImage imageNamed:@"venue .png"] forState:UIControlStateNormal];
CGRect btnOnclickFrame = sender.frame;
[aButton setBackgroundColor:[UIColor clearColor]];
btnOnclickFrame.size.width = 210;
sender.frame = btnOnclickFrame;
_lastSelectedTag = sender.tag; //Set lastSelectedButton to this button's tag
}
顺便提一下,您最好使用UITableView
而不是UIScrollView
还有两种可能性:
如果要创建3-4按钮,最好使用UISegmentedControl
,这将允许您对其进行自定义,并节省您编写所有额外代码的时间
如果创建多个按钮,则UITableView
可能更合适,并且使用UITableViewCell
的选定属性,则需要创建自定义UITableViewCell
希望这有帮助。单击按钮的事件,检查其标签。如果是第一个按钮,则将其背景色设置为红色,将所有其他设置为默认蓝色。像这样,您可以对所有其他按钮执行此操作。您已经在为按钮设置标记。如果你不使用它们又有什么用呢?(虽然事实上,我很难知道标签一点都不好)我可以用for(UIButton*btn in scrollVenue)
替换for(UIButton*aButton)
?是的,你可以给出任何名称,并在venueButtonClick方法中添加这个for循环。使用循环是多么可怕的方式啊!不,他正在根据数组的计数创建无限数量的按钮。虽然在这种情况下使用scrollView是不正确的,但是您的建议是不正确的。UITableView是我们所需要的,假设将有15个按钮。您将如何在iPhone宽度分段控制中适应这些特性?
-(void) venueButtonClick:(UIButton *)sender{
if(_lastSelectedTag!=80807652)
{
//lastSelectedTag is different than our safe value so it means that it now contains tag of real last selected button
UIButton *lastSelectedButton = [scrollVenue viewWithTag:_lastSelectedTag]; //This is the previously selected button
//Change it to blue and do whatever you want
[lastSelectedButton setBackgroundImage:nil forState:UIControlStateNormal];//remove background image
[lastSelectedButton setBackgroundColor:[UIColor colorWithRed:(6/255.0) green:(130/255.0) blue:(195/255.0) alpha:1]]; //give it color
}
[sender setBackgroundImage:[UIImage imageNamed:@"venue .png"] forState:UIControlStateNormal];
CGRect btnOnclickFrame = sender.frame;
[aButton setBackgroundColor:[UIColor clearColor]];
btnOnclickFrame.size.width = 210;
sender.frame = btnOnclickFrame;
_lastSelectedTag = sender.tag; //Set lastSelectedButton to this button's tag
}