-1

One UIButton when start the application it want to display "workout_heart.png". writing the button click action for that button. When the user click that button that image want to change "workout_heart_red.png". again click that button means that image become "workout_heart.png". it want to change alternate. How can I achieve this help me!!!

Code cracker
  • 3,105
  • 6
  • 37
  • 67
raja saran
  • 41
  • 9
  • Please go through existing questions, and better the documentation before. Look [HERE](http://stackoverflow.com/questions/1702194/how-to-use-a-uibutton-as-a-toggle-switch). You simply toggle images in the target method of your button. – n00bProgrammer Nov 25 '14 at 11:55
  • @n00bProgrammer That's not really constructive if you believe it is a duplicate then just mark it as one no need to be rude about it. I'm sure if we look at your questions when you were new we'd find that you would ask questions that wouldn't totally fit into the site (i.e. http://stackoverflow.com/questions/16231048/new-to-ios-development-need-resources) – Popeye Nov 25 '14 at 11:57
  • @Popeye, I did mark it as a duplicate. I added the comment, marked it as a duplicate, and moved on. Also, I apologise if I sounded rude, was not my intention. – n00bProgrammer Nov 25 '14 at 12:01
  • @n00bProgrammer This hasn't been marked as a duplicate? Did you do it through the close link? – Popeye Nov 25 '14 at 12:03
  • How? I 'm sober (pun intended), and I do remember marking it as a duplicate. Unsure why it wasn't marked. – n00bProgrammer Nov 25 '14 at 12:07

6 Answers6

1

Try this, declare sate as instance variable

- (IBAction)buttonTapped:(id)sender {
   if (state == 0) {
      state = 1;
      [btn setBackgroundImage:[UIImage imageNamed:@"workout_heart_red.png"] forState:UIControlStateHighlighted];
     }
    else if (state == 1){
       state = 0;
       [btn setBackgroundImage:[UIImage imageNamed:@"workout_heart.png"] forState:UIControlStateNormal];
     }
}
Parag Shinde
  • 176
  • 11
1
- (IBAction)btn_MyButton_Click:(id)sender
{
    if ([sender isSelected] == YES)
    {
        [sender setSelected:NO];
        [btn setBackgroundImage:[UIImage imageNamed:@"workout_heart.png"] forState:UIControlStateNormal];
    }
    else
    {
        [sender setSelected:YES];
         [btn setBackgroundImage:[UIImage imageNamed:@"workout_heart_red.png"] forState:UIControlStateNormal];
    }
}
Nikita Khandelwal
  • 1,741
  • 16
  • 25
0

Try this

[btn setBackgroundImage:[UIImage imageWithString:@"workout_heart.png" forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageWithString:@"workout_heart_red.png" forState:UIControlStateHighlighted];
Popeye
  • 11,839
  • 9
  • 58
  • 91
johny kumar
  • 1,270
  • 2
  • 14
  • 24
0

try this code

For button set the background image for selected and normal states like below

[<btn> setBackgroundImage:[UIImage imageNamed:@"workout_heart.png"] forState:UIControlStateNormal];
[<btn> setBackgroundImage:[UIImage imageNamed:@"workout_heart_red.png"] forState:UIControlStateSelected];

and write the action logic as below

- (IBAction)buttonTapped:(id)sender {
    UIButton *tempButton = (UIButton *)sender;
    if(tempButton.isSelected){
        [tempButton setSelected:NO];
    } else {
        [tempButton setSelected:YES];
    }
}
Ramesh Muthe
  • 811
  • 7
  • 15
0
- (void)viewDidLoad {
[super viewDidLoad];
// ...

self.btn1ImageName = @"workout_heart.png";
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(367, 117, 97, 25)];
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(toggleImage:)   forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}

- (void)toggleImage:(UIButton *)btn1 {
self.btn1ImageName = ([self.btn1ImageName isEqualToString:@"workout_heart.png"]) ? @"workout_heart_red.png" : @"workout_heart.png";
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
}
Sush
  • 1
  • 1
0

set background image by default in xib e.g "workout_heart.png" and then on checking state i.e

if(button.selected)
{
  [button setBackgroundImage:[UIImage imageNamed:"workout_heart_red.png"] forState: UIControlStateNormal];
}
else
{
[button setBackgroundImage:[UIImage imageNamed:"workout_heart.png"] forState: UIControlStateNormal];
}
Maul
  • 1,179
  • 6
  • 13
Sam
  • 198
  • 8