0

我在视图中动态创建了一行 numberButtons。单击任何数字时,按钮都会突出显示。如果我在该行中单击超过 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;
}
4

2 回答 2

1

我假设您的意思是您点击的每个按钮都会突出显示,而不会删除前一个突出显示。

一次只突出显示一个按钮。跟踪突出显示的按钮并在点击另一个按钮时删除其突出显示。

- (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 ...
}
于 2012-04-25T07:12:40.583 回答
0

最后通过调用取消选择方法覆盖您的选择方法。因此,当您单击时,您的控件将立即被选中和取消选中。

于 2012-04-25T07:26:06.453 回答