2017-02-01 3 views
0

私はUILabelsの配列を持っています。配列のUILabelの背景色を変更します

その後、この配列のラベルの1つの背景色を変更したいと思います。

NSLog(@"%@", [_tagArray lastObject]); 
UILabel *l = (UILabel *)[_tagArray lastObject]; 
[l setBackgroundColor:[UIColor redColor]]; 
[[_tagArray lastObject] setBackgroundColor:[UIColor redColor]]; 

ログには、作成したラベルが表示されますが、背景色は変更されません。

UILabel:0x15fec10c0;フレーム=(6 78; 52 29); text = 'hi'; clipsToBounds = YES; userInteractionEnabled = NO;層= < _UILabelLayer:0x174298240

+0

背景色が変化していないことをどのように知っていますか?投稿したコードは上手く見えます。 – rmaddy

+0

@rmaddy I '[self addSubview:tag];'それを私のビューに追加します。 – Peter

+0

あなたのコードが完全に正確で正しいと思われるので、あなたはsomemoreコードを投稿する必要があるかもしれません。 – bubibu

答えて

0

あなたが最後のオブジェクトを取り、ちょうど配列から最後のオブジェクトを置き換えることobject.Afterの色を変更したため

NSArray *_tagArray=[[NSArray alloc] initWithObjects:@"First",@"Second",@"Third",nil]; 
      for (int xOff=0; xOff<[_tagArray count]; xOff++) 
      { 
       UILabel *tag = [[UILabel alloc]initWithFrame:CGRectMake(xOff,xOff*50, 200, 50)]; 
       [tag setTextColor:[UIColor blueColor]]; 
       tag.numberOfLines = 0; 
       [tag setText:[_tagArray objectAtIndex:xOff]]; 
       [tag setTag:xOff]; 

       [self.view addSubview:tag]; 
      } 

      UILabel *l = (UILabel *)[self.view viewWithTag:1]; 
      [l setBackgroundColor:[UIColor greenColor]]; 

      UILabel *l2 = (UILabel *)[self.view viewWithTag:2]; 
      [l2 setBackgroundColor:[UIColor redColor]]; 
+0

問題のコードに何が間違っていたのかを説明し、この回答がどのように問題を解決するのか説明してください。コードのみの回答は、SOには適切ではありません。 – rmaddy

+0

@rmaddy投稿されたコードは、何が問題になったのかを理解するには不十分で、メインキューの問題や矛盾するタグである可能性があります。 –

-1
_tagArray = [_tagArray replaceObjectAtIndex:_tagArray.count-1 withObject:l]; 

使用このこのコードを試してみてください最後のコード行を削除します。

+0

これは必須ではなく、影響はありません。そして、このコードはコンパイルされません。 – rmaddy