我在 View 中动态创建了一行数字按钮。单击任何数字时,按钮都会突出显示。如果我单击该行中的多个按钮,则所有单击的按钮都会突出显示。如何避免多个按钮高度化?
我使用的代码如下
-(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 ...
}
关于objective-c - 如何使用标签为动态创建的按钮动态设置背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10310913/