Joe Joe - 3 months ago 8
Objective-C Question

How change the current image's button on Click?

In my file .h :

-(IBAction)Boutton:(id)sender;


In my file .m :

-(IBAction)Boutton:(id)sender
{

UIImage *btnImage1 = [UIImage imageNamed:@"x.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];

}


With this code i can change the image of my clicked button (sender).

The question is, how can I change the images of the others buttons (not the sender one) ?

For example if
(sender.tag == 4)
i would like To have something like :

-(IBAction)Boutton:(id)sender
{

UIImage *btnImage1 = [UIImage imageNamed:@"x.png"];
[sender setImage:btnImage1 forState:UIControlStateNormal];

UIImage *btnImage2 = [UIImage imageNamed:@"Y.png"];

[Boutton:(1) setImage:btnImage2 forState:UIControlStateNormal];
[Boutton:(2) setImage:btnImage2 forState:UIControlStateNormal];
[Boutton:(3) setImage:btnImage2 forState:UIControlStateNormal];

}

Answer

Simply link the other buttons to outlets of your UIViewController subclass. So that from -(IBAction)Boutton:(id)sender you can change their image by accessing them through their properties.