0

I have created the buttons using the loop and setting the properties and synthesize the buttons. Now i want to change the button color in the another view controller. I am setting the tag values for each button and i can get the tag values properly in another view controller, when select the buttons. Now i want to change the background color of each buttons.

Here it's sample code,

In CustomView.h

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

In 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;

}

In View controller:

            viewController.customBtn.backgroundColor = [UIColor blackColor];

It does affects all the buttons background color, so how can i change the background color of each buttons. If i am creating seperate instance for all the buttons and i can change the background color for the buttons. Using single instance of the buttons, how can we change the color of the button background.

Please Help me out.

Thanks!

Pugalmuni
  • 9,350
  • 8
  • 56
  • 97

5 Answers5

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];



}

By End of the loop you will have n number of buttons in buttonAry and each button will have unique tag.

You can read that array in another class

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

   UIColor *color =  [colorAry objectAtIndex:i];


  button.backgroundColor = [UIColor color];


}

In colorAry you can have different colors

thavasidurai
  • 1,972
  • 1
  • 26
  • 51
0

Use the viewWithTag method. e.g.

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

Maulik
  • 19,348
  • 14
  • 82
  • 137
0

Either you can do with viewWithTag.

Or, you can create an array of IBOutlets, then your work will be done.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

Either follow Anoop Vaidya, or use NSNotification to send your button object to another class, it'll hold all properties of your button.

josh
  • 1,681
  • 4
  • 28
  • 61
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, 
}
Irshad Mansuri
  • 397
  • 2
  • 12