0

UIButtons我有一个关于检查我是否全部被按下的最佳方法的快速问题。

我有 x 个UIButtons我以编程方式创建的。每个按钮都有自己独特的标签(从 100 开始向上递增。)

当你点击一个按钮时,运行这个:

- (void)myButtonAction:(id)sender
{
    [self handleButton:sender];
}

- (void)handleButton:(UIButton *)button
{
    // ???
}

当且仅当用户单击了所有按钮时,才希望实例[self allButtonsClicked]运行。

做这个的最好方式是什么?我应该做一个NSMutableArray, 并检查标签号是否在其中NSMutableArray,如果不是,则添加它。然后当 的NSMutableArray大小等于按钮的 x 个数时,运行[self allButtonsClicked]

确保单击每个按钮的最简单方法是什么?


*编辑我在输入后想通了。写出来帮助我得到它。


-(void)letterreveal: (id)sender {

    //data
    UIButton *button = (UIButton *)sender;

    //action
    [self clickcheck:[NSNumber numberWithInt:button.tag]];
}


-(void)clickcheck:(NSNumber*)currenttag {

    if ([self.buttonPressCounts containsObject:currenttag]) {
        NSLog(@"case A");
    }
    else {
        [self.buttonPressCounts addObject:currenttag];
         NSLog(@"case B");

        if([self.buttonPressCounts count]==[self.currentword length])
        {
            NSLog(@"fininshed");
        }
    }
}

buttonPressCounts 是一个 NSMutablearray。我只需要确保在制作按钮时设置它。

      self.buttonPressCounts = [NSMutableArray arrayWithCapacity:[self.currentword length]];

currentword 是一个 NSString(每个按钮都是从 NSString 派生的一个字母)。

4

2 回答 2

1

您可以创建一个NSMutableSet包含所有按钮的按钮,然后从该集合中删除每个单击的按钮,直到它为空。一旦集合为空,您肯定已经单击了所有按钮。

于 2012-05-11T11:19:49.490 回答
0

如果您不介意,如果按钮被按下一次或多次,请使用 NSMutableSet 的成员 ivar。

我会使用一些标签,但添加/删除按钮本身。

于 2012-05-11T11:19:35.683 回答