2012-04-25 118 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方法。 所以,當你點擊你的控件時,會立即被選中和取消選擇。