我已经以编程方式创建了UIButtons
并将其添加到我的UIScrollView
。现在我想将点击的UIButton
的背景图片更改为图片,其他人应该保持蓝色,这是默认颜色。现在当我点击另一个UIButton
时,我想将先前点击的UIButton
更改为蓝色且当前点击的按钮以获得bg图像。
这是用于创建和设置UIButton
属性的代码:
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;
}
这是按钮点击方法:
-(void) venueButtonClick:(UIButton *)sender{
[sender setBackgroundImage:[UIImage imageNamed:@"venue .png"] forState:UIControlStateNormal];
CGRect btnOnclickFrame = sender.frame;
btnOnclickFrame.size.width = 210;
sender.frame = btnOnclickFrame;
}
答案 0 :(得分:0)
修改您的venueButtonClick
,如下所示:
-(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]];
}
}
}
答案 1 :(得分:0)
在.h
文件中添加新属性。
@property NSUInteger lastSelectedTag;
在viewDidLoad
中,为其指定一些安全值:
_lastSelectedTag = 80807652;//example value as it will be unique
现在在IBAction
中,将其更改为:
-(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
。
答案 2 :(得分:0)
还有两种可能性:
UISegmentedControl
,这样您就可以自定义它并节省您必须编写所有额外代码的费用。See the apple docs for details on how to customise
UITableView
可能更合适,并使用UITableViewCell
的所选属性,则需要创建自定义UITableViewCell
。希望这有帮助。