Martin
Martin

Reputation: 2863

How to change the image of button when I click the button in IOS?

I am new to IOS , and I try to change the image of button when I click the button.

But the image of button doesn't change ,the code is like the following:

- (IBAction)modeChangeClick:(id)sender {
    NSLog(@"recordMode = %hhd" , recordMode);
    if(recordMode == YES){
        [self.modeChangeButton setImage:[UIImage imageNamed:@"photomode.png"] forState:UIControlStateNormal];

        [self.view addSubview:self.modeChangeButton];

        recordMode = NO;

    }else if(recordMode == NO){
        [self.modeChangeButton setImage:[UIImage imageNamed:@"recordmode.PNG"] forState:UIControlStateNormal];

        [self.view addSubview:self.modeChangeButton];

        recordMode = YES;
    }

}

Does there has something wrong in the above code ?

Thanks in advance.

------------------------EDIT------------------------

When I click the button , the image has change. But the first image I have add in Viewdidload is still exist...

The question 1

How to remove the first image I have add in Viewdidload when I click the button ???

The question 2

Can I fix the size of image when I click the button? (like same as the size of button or fix weight and height) ???

The picture at the left is nothing to do. The picture at the right is when I press the image of button. And it doesn't change any thing.

And the image change when I click the side of image(not on the image) enter image description hereenter image description here

Upvotes: 2

Views: 2896

Answers (6)

Preetham Baliga
Preetham Baliga

Reputation: 271

I see you are creating a button programmatically and you haven't set a target action for it.

So after UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)]; you need to add this: [modeChangeButton targetForAction:@selector(modeChangeClick:) withSender:modeChangeButton];

Hope this helps ! Cheers

Upvotes: 0

Abdullah Ossama Omari
Abdullah Ossama Omari

Reputation: 524

if you want you could just hide the image you dont want to display , and make another method with another UIButton that makes the other image visible and the other one hidden with simple code as follows:

- (IBAction)modeChangeClick:(id)sender {
//the images you want to display
UIImageView1.hidden = NO;
UIImageView2.hidden = YES;

//the buttons the user will press
UIButtonView1.hidden = NO;
UIButtonView2.hidden = YES;

//another method to change the images
- (IBAction)modeChangeTheImageClick:(id)sender {
 //the images you want to display
UIImageView1.hidden = YES;
UIImageView2.hidden = NO;

//the buttons the user will press
UIButtonView1.hidden = YES;
UIButtonView2.hidden = NO;            

}

Upvotes: 0

Manish Saini
Manish Saini

Reputation: 67

 change UIButton Image when Click
- (IBAction)clickToPutNameOnTheReport:(id)sender {

if ([sender isSelected]) {
    [sender setImage:[UIImage imageNamed:@"red.png"] forState:UIControlStateNormal];
   [sender setSelected:NO];

}
else{
    [sender setImage:[UIImage imageNamed:@"round.png"] forState:UIControlStateNormal];
     [sender setSelected:YES];

}

}

Upvotes: 0

Shruti
Shruti

Reputation: 1849

You said...But when I click on the image , the - (IBAction)modeChangeClick:(UIButton*)sender doesn't called.. Have you connected your action to the button in xib? and how are you setting the image to the button?

Upvotes: 1

Douglas Hill
Douglas Hill

Reputation: 1547

UISwitch is commonly used on iOS instead of toggle buttons.

If you really want a toggle button, you could use the button’s selected property (inherited from UIControl). Toggle the selected state in the button’s action method, and set the button’s image or background image for the state UIControlStateSelected.

Upvotes: 0

Pancho
Pancho

Reputation: 4143

Use this

- (IBAction)modeChangeClick:(id)sender {
    NSLog(@"recordMode = %hhd" , recordMode);

  [self.modeChangeButton setBackgroundImage: recordMode ? [UIImage imageNamed:@"photomode.png"] : [UIImage imageNamed:@"recordmode.png"] forState:UIControlStateNormal];

        [self.view addSubview:self.modeChangeButton];

        recordMode = !recordMode;


}

Upvotes: 1

Related Questions