2014-03-12 43 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。 ;-) –