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)
self.view
on every tap? – Mick MacCallum