0

ループを使用してボタンを作成し、プロパティを設定してボタンを合成しました。今、別のView Controllerでボタンの色を変更したいと思います。各ボタンのタグ値を設定しています。ボタンを選択すると、別のView Controllerでタグ値を適切に取得できます。次に、各ボタンの背景色を変更したいと思います。

これがサンプルコードです。

CustomView.h 内

    UIButton *customBtn;
    property (nonatomic, strong) UIButton *customBtn;
    @synthesize customBtn;

CustomView.m 内

for (int i=0; i<=[resultArray count]; i++)
{
     customBtn= [UIButton buttonWithType:UIButtonTypeCustom];
     customBtn = CGRectMake(X, 30, 20, 20); notsupport = [[UILabel alloc]initWithFrame:CGRectMake(25, 30, 290, 20)];
     [customBtn addTarget:customDelegate actionselector(MyAction:) forControlEvents:UIControlEventTouchUpInside];
     [self.view addSubview:customBtn];
     X = X + 30;

}

ビューコントローラーで:

            viewController.customBtn.backgroundColor = [UIColor blackColor];

すべてのボタンの背景色に影響するので、各ボタンの背景色を変更するにはどうすればよいですか。すべてのボタンに対して個別のインスタンスを作成している場合、ボタンの背景色を変更できます。ボタンの 1 つのインスタンスを使用して、ボタンの背景色を変更するにはどうすればよいでしょうか。

私を助けてください。

ありがとう!

4

5 に答える 5

2
for (int i=0; i<=[resultArray count]; i++)
{
    customBtn= [UIButton buttonWithType:UIButtonTypeCustom];
    customBtn = CGRectMake(X, 30, 20, 20); notsupport = [[UILabel alloc]initWithFrame:CGRectMake(25, 30, 290, 20)];
    [customBtn addTarget:customDelegate actionselector(MyAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:customBtn];
    X = X + 30;
    customBtn.tag = i;

   [buttonAry addObject:customBtn];



}

ループの終わりまでに、n個のボタンがbuttonAryあり、各ボタンには一意のタグがあります。

その配列を別のクラスで読み取ることができます

 for (int i=0; i<=[buttonAry count]; i++)
{
   UIButton *button = [buttonAry objectAtIndex:i];

   UIColor *color =  [colorAry objectAtIndex:i];


  button.backgroundColor = [UIColor color];


}

あなたはcolorAryさまざまな色を持つことができます

于 2012-12-28T07:47:52.220 に答える
0

どちらでもできますviewWithTag

または、の配列を作成するとIBOutlets、作業が完了します。

于 2012-12-28T07:36:42.337 に答える
0

Anoop Vaidya に従うか、NSNotification を使用してボタン オブジェクトを別のクラスに送信すると、ボタンのすべてのプロパティが保持されます。

于 2012-12-28T07:39:37.043 に答える
0
for (int i=0; i<=[resultArray count]; i++)
{
   customBtn= [UIButton buttonWithType:UIButtonTypeCustom];
   customBtn = CGRectMake(X, 30, 20, 20); notsupport = [[UILabel alloc]initWithFrame:CGRectMake(25, 30, 290, 20)];
  [customBtn addTarget:customDelegate actionselector(MyAction:) forControlEvents:UIControlEventTouchUpInside];

  customBtn.tag = i;

  [self.view addSubview:customBtn];
  X = X + 30;

}

-(IBAction)MyAction:(id)sender{

  UIButton *btn = (UIButton *)sender;

  NSLog:(@"tag:%d"btn.tag);
  // here you find your specific button, 
}
于 2012-12-28T07:43:06.640 に答える
0

メソッドを使用しviewWithTagます。例えば

UIButton *button = (UIButton *)[viewController.view viewWithTag:aTag];

于 2012-12-28T07:33:04.223 に答える