2014-12-04 66 views
1

我在scrollView中有n個按鈕。如果點擊一個按鈕,它的文本顏色會改變,但如果我點擊另一個按鈕,上一個按鈕的文本顏色保持不變。我想將上一個按鈕文字顏色更改爲默認顏色,同時單擊另一個按鈕。行爲將像分段控制一樣。請幫我這方面,我下面提供我的代碼:更改按鈕文本顏色以編程方式爲n個按鈕而不更改其他按鈕的文本顏色

-(void) loadScrollView:(CGRect)scrollViewFrame withButtonArray:(NSArray*)buttonArray withCase: (int)ButtonCase 
{ 
    scrollView=[[UIScrollView alloc]initWithFrame:scrollViewFrame]; 
    [scrollView setScrollEnabled:YES]; 
    [scrollView setBackgroundColor:[UIColor blackColor]]; 
    [scrollView setContentSize:CGSizeMake(100 * 768, 40)]; 

    for (int i = 0; i < [buttonArray count]; i++) 
    { 
     adButtonOutLet = [[UIButton alloc] initWithFrame:CGRectMake(140*i, 0, 135, 40)]; 
     if (ButtonCase==0) { 
      [adButtonOutLet setBackgroundColor:UIColorFromRGB(0X272c2f)]; 
      [adButtonOutLet setTitleColor:UIColorFromRGB(0x969696) forState:UIControlStateNormal]; 
     } 
     else 
     { 
      if (i==0) { 
       adButtonOutLet.backgroundColor=UIColorFromRGB(0x000000) ; 
       [adButtonOutLet setTitleColor:UIColorFromRGB(0x179d95) forState:UIControlStateNormal]; 
      }    
     } 

     adButtonOutLet.titleLabel.font=[UIFont fontWithName:@"MyriadPro" size:14.0]; 
     [adButtonOutLet setTitle:[buttonArray objectAtIndex:i] forState:UIControlStateNormal]; 
     adButtonOutLet.userInteractionEnabled= YES; 
     [adButtonOutLet setTag:i]; 
     [adButtonOutLet addTarget:self action:@selector(adButtonAction:) forControlEvents:UIControlEventTouchUpInside]; 

     [scrollView addSubview:adButtonOutLet]; 
     [self.view addSubview:scrollView]; 
    } 
} 

這裏是我的操作方法:

-(void)adButtonAction:(UIButton*)sender 
{ 
    for (int i = 0; i < [menuArray count]; i++) 
    { 
     int prevTag = 0; 
     if (sender.tag == i && Case==0) 
     {     
      [self reloadScrollViewwithButtonTag:i]; 
      // [sender setSelected:YES]; 
      sender.backgroundColor=UIColorFromRGB(0x000000) ; 
      [sender setTitleColor:UIColorFromRGB(0x179d95) forState:UIControlStateNormal]; 
      prevTag=i; 
     } 

     if (Case==1) { 
      sender.backgroundColor=UIColorFromRGB(0x000000) ; 
      [sender setTitleColor:UIColorFromRGB(0x179d95) forState:UIControlStateNormal]; 
      if (sender.tag==prevTag-1) { 
       [sender setBackgroundColor:UIColorFromRGB(0X272c2f)]; 
       [sender setTitleColor:UIColorFromRGB(0x969696) forState:UIControlStateNormal]; 
      } 
     }    
    } 
} 

回答

2

你爲什麼不嘗試改變所有的按鈕樣式沒有選擇,除了發件人參數(選定按鈕)上的那個?

-(void)adButtonAction:(UIButton*)sender{ 
    for (int i = 0; i < [menuArray count]; i++) 
    { 
     if (sender == menuArray[i]) 
     { 
      //Selected code style 
     } 

     else{ 
      //No selected code style 
     } 
    } 
} 

考慮到menuArray是一個包含所有按鈕的數組。 通過這種方式,您可以在按下按鈕時檢查和修改所有樣式。

希望這可以幫助您或最終爲您解決問題提供線索。

1

我不明白adButtonAction(什麼是menuArray?)方法中的所有內容,但我認爲您所需要的很簡單,只需將其適用於您的方法即可。

首先創建一個NSMutableArray,讓您的按鈕列表參考:

for (int i = 0; i < [buttonArray count]; i++) 
{ 
    adButtonOutLet = [[UIButton alloc] initWithFrame:CGRectMake(140*i, 0, 135, 40)]; 
    [myButtonArray addObject:adButtonOutlet]; 
.... 

然後在你的操作方法,設置合適的顏色:

for (UIButton* b in myButtonArray){ 
    if(b.tag == sender.tag){ 
     [self setSelectedColor:YES forButton:b]; 
     // Do what you want here 
    } 
    else{ 
     [self setSelectedColor:NO forButton:b]; 
    } 
} 

有了:

-(void)setSelectedColor:(BOOL)selected forButton:(UIButton)button{ 
    if(selected){ 
     sender.backgroundColor=UIColorFromRGB(0x000000) ; 
     [sender setTitleColor:UIColorFromRGB(0x179d95) forState:UIControlStateNormal]; 
    } 
    else{ 
     [sender setBackgroundColor:UIColorFromRGB(0X272c2f)]; 
     [sender setTitleColor:UIColorFromRGB(0x969696)forState:UIControlStateNormal]; 
    } 
} 
1
  • 應用狀態老虎鉗(正常,選定)t itle顏色的按鈕。
  • 只需堅持選定的按鈕引用爲弱。
  • 當用戶點擊按鈕時,將按鈕狀態設置爲選中,而不是正常。
  • 更改上次選擇的按鈕狀態正常改爲選中

看看下面的代碼,將幫助您

UIButton *selectedButton; 
-(void)adButtonAction:(UIButton*)sender 
{ 
     UIButton *tempButton = sender; 
     if (selectedButton && selectedButton!=tempButton) 
     { 
      [selectedButton setSelected:NO]; 
     } 
     [tempButton setSelected:YES]; 
     selectedButton = tempButton; 
}