2012-04-25 113 views
0

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

你如何“突出显示”按钮? – 2012-04-25 07:24:11

回答

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 ... 
} 
+0

对不起......我无法使用此方法解决 – ani 2012-04-25 08:22:34

+0

您需要一个属性来存储lastSelectedButton:@property(非原子,弱)UIButton * lastSelectedButton;'并合成它:'@synthesize lastSelectedButton = _lastSelectedButton; '能够设置和检索最后选择的按钮 – 2012-04-25 09:26:30

+0

是的,我解决了。 – ani 2012-04-25 09:33:54

0

最后调用deselect方法覆盖你的select方法。 所以,当你点击你的控件时,会立即被选中和取消选择。