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

タグから 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 に答える