首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >比较一个数组目标-c中的两个UIButton.tag元素

比较一个数组目标-c中的两个UIButton.tag元素
EN

Stack Overflow用户
提问于 2014-09-30 17:22:13
回答 1查看 365关注 0票数 0

我有一个名为“_orderOfCardPlacement”的数组,它在游戏的每一回合之后都会将对象放入其中。这些对象是UIButtons,它包含许多细节,如setImage、setBounds、setCenter,最重要的是对于这个问题setTag。对于setTag来说,有两种可能的结果--“1”或“2”,它们最初是用一个或另一个设置的,这可以通过另一种方法来改变,这种方法称为每一个转弯。我需要做的是查看这个数组,看看有多少次“1”出现,多少次“2”出现,然后比较这个结果。因此,如果“1”出现9次,“2”出现7次,那么“1”在这种情况下就会获胜。因此,当我NSLog我的数组(比如在一轮之后)时,我得到了以下内容:

代码语言:javascript
复制
"<OBShapedButton: 0x7ff6f968e6a0; baseClass = UIButton; frame = (103 387.5; 150 135); opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; tag = 1; layer = <CALayer: 0x7ff6f968e8a0>>"

随后的循环将向数组中添加更多的按钮。但是我需要知道,一旦这个数组满了,游戏完成了,如何访问对象的'tag =‘部分进行比较。谢谢您的建议,如果有必要,我会添加更多的代码!

这是一个完整的解决方案,如果有一个更优雅的方法来做我在这里做的事情,绝对有兴趣知道!

代码语言:javascript
复制
//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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-30 18:16:06

我不知道我是否清楚你想要达到的目标。如果您的按钮存储在NSMutableArray中,您可以通过获取第一个元素(在for-循环中)检查它们的标记并应用转换。

代码语言:javascript
复制
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
}

我希望我能很好地理解它,这可以帮助你。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26126569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档