[英]how to set background image dynamically for dynamically created button using tag?
I have created a row of numberButtons inside a view dynamically.I am getting button highlighted when clicking any number.If I am clicking more than 1 in that row ,all of the clicked buttons get highlighted.What to do for avoiding multiple highlation? 我在视图中动态创建了一行numberButtons。单击任何数字时都会使按钮高亮显示。如果我在该行中单击多个,则所有单击的按钮都会高亮显示。如何避免多重高阶?
I have used the code as follows 我使用了如下代码
-(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;
}
I'm assuming that you mean that every button you tap is highlighted without removing the previous highlight. 我假设您的意思是您轻触的每个按钮都将突出显示而不会删除前一个突出显示。
To only have one button highlighted at a time. 一次仅突出显示一个按钮。 Keep track of what button was highlighted and remove its highlight when tapping another button.
跟踪突出显示了哪个按钮,并在点击另一个按钮时将其突出显示移除。
- (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 ...
}
Override your select method by calling deselect method in the end. 最后通过调用deselect方法来覆盖您的select方法。 So, when you click your control will be selected and deselected instantly.
因此,当您单击控件时,将立即选择和取消选择控件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.