Search code examples
xcodeuiimageibaction

Xcode change image on button that is not sender


I am trying to change a image of a button that is not the senders, I have tried to link the code to the buttons and it seems not to work I tried to link the 3 buttons to the -(IBAction)Button1:(id)sender; it still does not work.

.h

-(IBAction)Button1:(id)sender;

-(IBAction)ButtonUn1:(id)sender;

-(IBAction)ButtonDef1:(id)sender;

.m

-(IBAction)Button1:(id)sender

{
    UIImage *btnImage1 = [UIImage imageNamed:@"button_Not_At_all_R.png"];

    [sender setImage:btnImage1 forState:UIControlStateNormal];

    UIImage *btnImage2 = [UIImage imageNamed:@"Button_Unsure.png"];
    UIImage *btnImage3 = [UIImage imageNamed:@"Button_Definitely.png"];

    [ButtonUn1 setImage:btnImage2 forState:UIControlStateNormal];
    [ButoonDef1 setImage:btnImage3 forState:UIControlStateNormal];
}

Thanks for any help.


Solution

  • You need to create an IBOutlet for your button and link it to access it directly:

    IBOutlet UIButton* buttonToChangePicture;
    

    The IBAction ist only used to access an event.

    Edit:

    Another option is to use viewWithTag.

    UIButton* button = (UIButton*)[myView viewWithTag:555]
    

    You need to set a unique tag for your button first (555 in my example) There is no other way to access another button in an IBAction