我有一个名为“_orderOfCardPlacement”的数组,它在游戏的每一回合之后都会将对象放入其中。这些对象是UIButtons,它包含许多细节,如setImage、setBounds、setCenter,最重要的是对于这个问题setTag。对于setTag来说,有两种可能的结果--“1”或“2”,它们最初是用一个或另一个设置的,这可以通过另一种方法来改变,这种方法称为每一个转弯。我需要做的是查看这个数组,看看有多少次“1”出现,多少次“2”出现,然后比较这个结果。因此,如果“1”出现9次,“2”出现7次,那么“1”在这种情况下就会获胜。因此,当我NSLog我的数组(比如在一轮之后)时,我得到了以下内容:
"<OBShapedButton: 0x7ff6f968e6a0; baseClass = UIButton; frame = (103 387.5; 150 135); opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; tag = 1; layer = <CALayer: 0x7ff6f968e8a0>>"随后的循环将向数组中添加更多的按钮。但是我需要知道,一旦这个数组满了,游戏完成了,如何访问对象的'tag =‘部分进行比较。谢谢您的建议,如果有必要,我会添加更多的代码!
这是一个完整的解决方案,如果有一个更优雅的方法来做我在这里做的事情,绝对有兴趣知道!
//set the amount of cards each player has
_howManyCardsForComputer = 0;
_howManyCardsForPlayer = 0;
for (int i = 0; i < _orderOfCardPlacement.count; i++) {
UIButton *auxButton = (UIButton *) [_orderOfCardPlacement objectAtIndex:i];
NSInteger playerScore = auxButton.tag;
NSMutableArray *totalScoreArray = [[NSMutableArray alloc] init];
[totalScoreArray addObject:[NSNumber numberWithInteger:playerScore]];
for (int j = 0; j < totalScoreArray.count; j++) {
if ([[totalScoreArray objectAtIndex:j] isEqualToNumber:[NSNumber numberWithInteger:1]]) {
_howManyCardsForPlayer++;
}
if ([[totalScoreArray objectAtIndex:j] isEqualToNumber:[NSNumber numberWithInteger:2]]) {
_howManyCardsForComputer++;
}
}
}
_playerScore.text = [NSString stringWithFormat:@"Players cards %i", _howManyCardsForPlayer];
_playerScore.hidden = NO;
_computerScore.text = [NSString stringWithFormat:@"Computers cards %i", _howManyCardsForComputer];
_computerScore.hidden = NO;发布于 2014-09-30 18:16:06
我不知道我是否清楚你想要达到的目标。如果您的按钮存储在NSMutableArray中,您可以通过获取第一个元素(在for-循环中)检查它们的标记并应用转换。
NSMutableArray *arrayWithButtons = [[NSMutableArray alloc] init];
//... Fill the array
//Check tags
for(int i=0; i<numButtons; i++){
UIButton *auxButton = (UIButton*) [arrayWithButton objectAtIndex:i];
int auxTag = auxButton.tag;
//... perform the proper operations
}我希望我能很好地理解它,这可以帮助你。
https://stackoverflow.com/questions/26126569
复制相似问题