以编程方式链接按钮

时间:2014-03-12 14:46:52

标签: ios ios7 uibutton

我正在设计一个应用程序,要求我有一个按钮网格,每个按钮都有四种不同的状态。未选中,已选中,已命中或未命中。我正在尝试以编程方式生成这些按钮,但我只是在保留其中一个选项时遇到问题。有没有办法可以将它们放入一个数组然后迭代数组以取消选择它们?我查看了IBOutletCollections,但这些不起作用,因为我想以编程方式创建这些按钮

2 个答案:

答案 0 :(得分:0)

我想也许您应该查看我在 为iPhone和iPad开发iOS 7应用程序cs193p 中学到的演示。

https://github.com/elilien/Matchismo

答案 1 :(得分: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++;
}
}