user6183984
user6183984

Reputation: 75

How do I change a button's color when pressed and reset to original color when a different button are pressed?

I have created buttons dynamically based on array count ,if i pressed it will move to next page .i want to change the background color of the button if it is pressed .i pressed 1st button its background color should changed ,and den if i pressed anyother buttons the first pressed button should get into default color of the button , and the new pressed button's background color should changed , please help me to do this ,On button clicked method i have tried like this ,

 - (IBAction)btn1Tapped:(id)sender {

UIButton *btn = (UIButton *) sender;


selected = YES;

if (selected) {
    [btn setBackgroundColor:[UIColor redColor]];

} 

}

and this my button creation code ,

int buttonheight = 30;
int horizontalPadding = 20;
int verticalPadding = 20;

int totalwidth = self.view.frame.size.width;

int x = 10;
int y = 150;

for (int i=0; i<array.count; i++)
{

    NSString* titre = [array objectAtIndex:i];


    CGSize contstrainedSize = CGSizeMake(200, 40);//The maximum width and height

    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont systemFontOfSize:20.0], NSFontAttributeName,
                                          nil];
    CGRect frame = [titre boundingRectWithSize:contstrainedSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDictionary context:nil];

    int xpos = x + CGRectGetWidth(frame);




    if (xpos > totalwidth) {

        y =y +buttonheight+ verticalPadding;
        x = 10;


    }

    UIButton *word= [UIButton buttonWithType:UIButtonTypeRoundedRect];

    self.word  = word;


    NSLog(@"%@", NSStringFromCGRect(frame));
    word = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    word.frame = CGRectMake(x, y, CGRectGetWidth(frame)+5, CGRectGetHeight(frame));
    [word setTitle:titre forState:UIControlStateNormal];
    [word setTitle:titre  forState:UIControlStateSelected];

    word.backgroundColor = [UIColor colorWithRed:30.0/255.0 green:134.0/255.0 blue:255.0/255.0 alpha:1.0];

    [word setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];


    [word setTag:i];
    [word addTarget:self action:@selector(btn1Tapped:) forControlEvents:UIControlEventTouchUpInside];
    word.layer.borderColor = [UIColor blackColor].CGColor;
    word.layer.borderWidth = 1.0f;
    word.layer.cornerRadius = 5;

    [self.view addSubview:word];

    x =x+horizontalPadding+CGRectGetWidth(frame);



}

Upvotes: 0

Views: 877

Answers (3)

Ajay Gabani
Ajay Gabani

Reputation: 1178

- (IBAction)allBtnSharedTappedevent:(id)sender {

     UIButton *btn = (UIButton *) sender;

     [btn setBackgroundColor:[UIColor redColor]];

    //loop through all your buttons

    for(UIView *view in [self.view subviews]){
        if([view isKindOfClass:[UIButton class]]){
            if(view != btn){
                UIButton* btn1 = (UIButton*) view;
                [btn1 setBackgroundColor:[UIColor grayColor]];
            }
        }
    }
}

Upvotes: 1

bdjett
bdjett

Reputation: 524

Keep the buttons in an array.

@property (nonatomic, copy, readonly) NSArray<UIButton *> *buttons;

Then in your method that handles the tap, do something like this.

- (IBAction)buttonTapped:(UIButton *)sender {
    // Loop through all buttons, clearing the background color
    for (UIButton *button in self.buttons) {
        button.backgroundColor = [UIColor clearColor];
    }
    // Set the background color for the selected button
    sender.backgroundColor = [UIColor redColor];
}

You should avoid using the tag property of UIView, it will just cause you headaches down the road.

Upvotes: 1

KDeogharkar
KDeogharkar

Reputation: 10959

give your button tag number like 1,2,3,4 ... etc then

- (IBAction)btn1Tapped:(id)sender {

UIButton *btn = (UIButton *) sender;

 [btn setBackgroundColor:[UIColor redColor]];
loop through all your buttons

for(int i = 1;i <= total numbers of button; i++){

if(btn.tag != i){

  UIButton* btn1 = [myView viewWithTag:i];
[btn1 setBackgroundColor:[UIColor graycolor]];
}

}

}

Upvotes: -1

Related Questions