如何使用标签为动态创建的按钮动态设置背景图像?

时间:2012-04-25 07:08:09

标签: objective-c ios xcode4

我在视图中动态创建了一行numberButtons。当点击任意数字时,我突出显示按钮。如果我在该行中点击多于1,则所有单击的按钮都会突出显示。如何避免多个highlation?

我使用了以下代码

-(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;
}

2 个答案:

答案 0 :(得分: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 ...
}

答案 1 :(得分:0)

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

相关问题