我正在设计一个应用程序,它要求我有一个按钮网格,每个按钮都有四个不同的状态。未选定、选定、命中或错过。我试图通过编程生成这些按钮,但是我很难只选择其中一个按钮。是否有一种方法可以将它们放入数组中,然后迭代数组以取消它们?我查看了IBOutletCollections,但是这些都无法工作,因为我想以编程方式创建这些按钮。
发布于 2014-04-11 19:38:22
这适用于我,其中_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++;
}
}发布于 2014-03-12 15:22:33
我认为也许您应该检查一下我在开发iPhone和iPad cs193p的iOS 7应用程序中学到的这个演示。
https://stackoverflow.com/questions/22355105
复制相似问题