user3349668
user3349668

Reputation: 147

Change button image by clicking on the button IOS7

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

Answers (5)

Indrajeet
Indrajeet

Reputation: 5666

use (IBAction)click:(UIButton *)sender

instead of

(IBAction)click:(id)sender 

Upvotes: 1

Kanhaiya
Kanhaiya

Reputation: 249

Try this

[btn setBackgroundImage:[UIImage imageNamed:@"vv.png"] forState:UIControlStateNormal];

Upvotes: 2

dariaa
dariaa

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

Dattatray Deokar
Dattatray Deokar

Reputation: 2103

Try this

- (IBAction)click:(id)sender {

    UIButton *btn = (UIButton*)sender;

    [btn setImage:[UIImage imageNamed:@"vv.png"] forState:UIControlStateNormal];
}

Upvotes: 0

Anil Varghese
Anil Varghese

Reputation: 42977

Try UIControlStateNormal

[btn setBackgroundImage:[UIImage imageNamed:@"vv.png"] forState:UIControlStateNormal];

Upvotes: 2

Related Questions