2014-03-12 203 views
0

我正在設計一個應用程序,要求我具有四種不同狀態的按鈕網格。 Unselected,Selected,Hit或Miss。我試圖以編程方式生成這些按鈕,但我遇到了麻煩,只保留其中一個按鈕。有沒有辦法,也許我可以把它們放入一個數組,然後迭代數組以取消選擇它們?我看着IBOutletCollections但這些不會起作用,因爲我要創建這些按鈕編程以編程方式鏈接的按鈕

+2

聽起來像是你已經知道答案: 「把它們放到一個數組中,然後迭代數組以取消選擇它們」 - 這應該起作用 – dariaa

+0

NSArray引用是「強壯的」。 –

回答

0

這爲我工作,其中_lastSelectedButton是最後的選擇按鈕的ID,寬度是25

-(void)unselect 
{ 
if(_lastSelectedButton){ 
    //Unselect the last selected button However only change its background if is blue 
    [[_lastSelectedButton layer] setBorderWidth:0.0f]; 
    if([_lastSelectedButton.titleLabel.text isEqual: @"0"]){ 

     [[_lastSelectedButton layer] setBackgroundColor:[missColor CGColor]]; 
    }else if([_lastSelectedButton.titleLabel.text isEqual: @"/"]){ 
     [[_lastSelectedButton layer] setBackgroundColor: [hitColor CGColor]]; 
    } 
    else{ 
     [[_lastSelectedButton layer] setBackgroundColor:unselectedColor.CGColor]; 
    } 
} 
_lastSelectedButton = nil; 

} 
- (void)advanceSelection:(UIButton*)sender 

{

int i = 0; 
//Find the indice that the present selected thing is 
for(UIButton *button in btnList) 
{ 
    if(sender == button) 
    { 
     if((i >= (self.numberOfPlayers - 1) * width) &&((i + 1) % 5 == 0)){ 
      //Then animated scroll to the next frame 
      if (i + 1 == self.numberOfPlayers * width) { 
       //then it is the last button 
       [self unselect]; 


      }else{ 
       CGFloat x = (([_pageControl currentPage] + 1.0) * 320.0); 
       [_scrollView setContentOffset:CGPointMake(x, 0.0f) animated:YES]; 
       [self selection:btnList[i - (width * (self.numberOfPlayers - 1)) + 1]]; 
       [self scrollViewDidEndDecelerating:self.scrollView]; 
       self.pageControl.currentPage = _pageControl.currentPage + 1; 
      } 
     }else if(i >= ((self.numberOfPlayers - 1) * width)){ 
      //We want to move it up one 
      //subtract width times hieght and ad one 
      [self selection:btnList[i - (width * (self.numberOfPlayers - 1)) + 1]]; 

     }else{ 
      //To go one down add 25 (the width) 
      [self selection:btnList[i + 25]]; 
     } 
    } 
    i++; 
} 
} 
0

我想,也許你應該檢查這個演示中,我已經學會了在開發的iOS 7應用爲iPhone和iPad cs193p

https://github.com/elilien/Matchismo

+0

我會去看看是否有幫助 – Austin

+0

祝你好運2 u。 ;-) –