2
votes

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

  • (void)viewDidLoad {

    UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)]; [modeChangeButton setBackgroundImage:[UIImage imageNamed:@"recordmode.PNG"] forState:UIControlStateNormal];

    [self.view addSubview:modeChangeButton];
    

    }

  • (IBAction)modeButtonClick:(id)sender { if (recordMode == YES) {

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

    }else if (recordMode == NO){

    [self.modeChangeButton setBackgroundImage:[UIImage imageNamed:@"file_viewer.png"] forState:UIControlStateNormal];
    recordMode = YES;
    

    } }

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

6
Why are you re-adding the button as a subview of self.view on every tap?Mick MacCallum
So...it doesn't need to use self.view addSubview:self.modeChangeButton]; ???Martin

6 Answers

1
votes

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;


}
1
votes

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?

0
votes

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.

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

}

}

0
votes

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;            

}
0
votes

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