1

我正在从 动态NSMutableArray创建标签。在创建标签时,我为每个标签设置了标签。我有一个NSMutableArray名为wordArray. 现在,我想检查我的字符串在 wordArray 中是否可用,我可以使用以下命令进行检查:

[wordArray containsObject:wordStr];

动态创建标签:

UILabel *wordLabl;
int tagValue3 = 1;
for (int iloop = 0; iloop < [wordArray count]; iloop++)
{

     wordLabl = [self addwordLabelRect:CGRectMake(80 * iloop + 20, 420 , 100,      20)andTag:tagValue3];//30 + 35 30 * iloop+
     [self.view addSubview:wordLabl];
     tagValue3 += 1;
}

-(UILabel *)addwordLabelRect:(CGRect)rect andTag:(int)integerValue
{
wordLabel = [[UILabel alloc] init];
wordLabel.frame = rect;
wordLabel.userInteractionEnabled = YES;
wordLabel.tag = integerValue;
wordLabel.backgroundColor = [UIColor clearColor];
wordLabel.font = [UIFont systemFontOfSize:15];
wordLabel.text = [NSString stringWithFormat:@"%@",[wordArray objectAtIndex:integerValue - 1]];
wordLabel.textAlignment = NSTextAlignmentCenter;
wordLabel.textColor = [UIColor whiteColor];

return wordLabel;
}

使用上面的代码我正在创建标签和标签。但是,如果wordArray包含我想要更改该标签的字符串。我textColor认为这可以使用 Tag 来完成,但我怎样才能获得标签的标签值。

4

3 回答 3

1

抱歉,我忽略了您的代码...您只需在要访问相应标签的位置添加以下行:

if([wordArray containsObject:wordStr])
{
UILabel *label = (UILabel *) [self.view viewWithTag:([wordArray indexOfObject:wordStr] - 1)];//since u started tag assignment from 1
label.textcolor = [UIColor yellowColor];
}
于 2013-08-27T09:04:40.087 回答
0

我猜你正在做类似的事情来设置标签?

for (NSUInteger i = 0; i < [wordArray count]; ++i) {
    UILabel * label;
    // setup your label...
    [label setTag:i];
    [yourView addSubview:label];
}

如果是这样,只需执行以下操作:

NSUInteger index = [wordArray indexOfObject:wordStr];
if (index != NSNotFound) {
    UILabel * label = [yourView viewWithTag:index];
    // do whatever you want with your label
}

祝你好运。

于 2013-08-27T07:26:05.900 回答
0

如果你想从它的标签中获取 UILabel。您可以使用以下循环

int i=0;
for (NSObject *view in self.View.subviews) 
{
    if ([view isKindOfClass:[UILabel class]]) 
    {
      label = (UILabel *)[[self view] viewWithTag:wordArray[i]];
        NSLog(@"%@",label.text);
       //here you get your label
    }
  i++;
}
于 2013-08-27T07:26:51.933 回答