如何使用标签动态创建按钮动态设置背景图像?
问题描述:
我在视图内动态创建了一行数字按钮。点击任何数字时,我都会突出显示按钮。如果我在该行中单击的次数超过1,则所有单击的按钮都会突出显示。避免多次高亮显示的操作?如何使用标签动态创建按钮动态设置背景图像?
我已经使用的代码如下
-(void)pressed:(id)sender{
UIButton *button = (UIButton *)sender;
if(!button.selected){
[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(highlightButton:) userInfo:button repeats:NO];
} else {
[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(unhighlightButton:) userInfo:button repeats:NO];
}
-(void)highlightButton:(id)sender{
UIButton *button = (UIButton *)[sender userInfo];
button.highlighted = YES;
button.selected = YES;
}
-(void)unhighlightButton:(id)sender{
UIButton *button = (UIButton *)[sender userInfo];
button.highlighted = NO;
button.selected = NO;
}
答
我假设你的意思是你每次敲击按键无需删除先前亮点突出。
一次只能突出显示一个按钮。跟踪突出显示哪个按钮,并在点击另一个按钮时删除突出显示。
- (void)buttonTapped:(UIButton *)button {
if (button != [self lastSelectedButton]) { // don't re-highlight the same button
// remove the highlight of "lastSelectedButton"
[self setLastSelectedButton:button];
// add the highlight to "lastSelectedButton" (not updated to the new button)
}
// Do the rest of you button logic here ...
}
答
最后调用deselect方法覆盖你的select方法。 所以,当你点击你的控件时,会立即被选中和取消选择。
你如何“突出显示”按钮? – 2012-04-25 07:24:11