Reputation: 147
I'm to trying change the button image after tapping on it but its not working.
- (IBAction)click:(id)sender {
UIButton *btn = (UIButton*)sender;
[btn setBackgroundImage:[UIImage imageNamed:@"vv.png"] forState:UIControlStateHighlighted];
}
I have seen the many solutions which given here its not working for me please tell me is there any other way to make it done.
Upvotes: 0
Views: 548
Reputation: 5666
use (IBAction)click:(UIButton *)sender
instead of
(IBAction)click:(id)sender
Upvotes: 1
Reputation: 249
Try this
[btn setBackgroundImage:[UIImage imageNamed:@"vv.png"] forState:UIControlStateNormal];
Upvotes: 2
Reputation: 6385
If you are after something like "toggle button" you can use the following code
- (IBAction)click:(id)sender {
UIButton *btn = (UIButton*)sender;
btn.selected = !btn.selected;
}
previously setting up the image for the selected state in nib
/storyboard
or programmatically:
- (void)viewDidLoad {
[super viewDidLoad];
[self.btn setBackgroundImage:[UIImage imageNamed:@"vv.png"] forState:UIControlStateSelected];
}
This way if you press the button again it will get back to the original image.
If you want to change image to "vv.png" for good then just use:
[btn setBackgroundImage:[UIImage imageNamed:@"vv.png"] forState:UIControlStateNormal];
Upvotes: 1
Reputation: 2103
Try this
- (IBAction)click:(id)sender {
UIButton *btn = (UIButton*)sender;
[btn setImage:[UIImage imageNamed:@"vv.png"] forState:UIControlStateNormal];
}
Upvotes: 0
Reputation: 42977
Try UIControlStateNormal
[btn setBackgroundImage:[UIImage imageNamed:@"vv.png"] forState:UIControlStateNormal];
Upvotes: 2