Search code examples
iosobjective-cuibuttonuiswitch

Changing UIButton background image on event


When a UISwitch changes from 'OFF' to 'ON', I want the background image of a UIButton to change. I have the following code:

- (IBAction)switchValueChanged {
    if (Hard1ON.on) {
        UIImage *buttonImage = [UIImage imageNamed:@"red.jpg"];
        [Hard1 setImage:buttonImage forState:UIControlStateNormal];
        [self.view addSubview:Hard1];
        NSLog(@"Change");
    }
    else {
        UIImage *buttonImage = [UIImage imageNamed:@"white.jpg"];
        [Hard1 setImage:buttonImage forState:UIControlStateNormal];
        [self.view addSubview:Hard1];
    }
}

When I hit the switch, the console logs correctly so I know that's working, however the background of the button doesn't change.

For reference, Hard1ON is my UISwitch and Hard1 is my UIButton.


Solution

  • Add your button once, the most common way is to add it in viewDidLoad and then change its image when you perform the switching.

    So, inside viewDidLoad method

    // create the button
    [self.view addSubview:Hard1];
    

    then, in your value changed method

    if (Hard1ON.on) {
        UIImage *buttonImage = [UIImage imageNamed:@"red.jpg"];
        [Hard1 setImage:buttonImage forState:UIControlStateNormal];
        NSLog(@"Change");
    } else {
        UIImage *buttonImage = [UIImage imageNamed:@"white.jpg"];
        [Hard1 setImage:buttonImage forState:UIControlStateNormal];
    }
    

    NOTE use camelCase notation for naming variables.

    For example, Hard1 should look like hard1. It would be better to name it as hard1Button or similar. The same applies for the switch element. Like for example, hard1OnSwicth.

    Edit

    I would try to create the button programmatically in viewDidLoad method.

    - (void)viewDidLoad
    { 
        [super viewDidLoad];
    
        self.hardButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.hardButton setFrame:CGRectMake(0, 0, 200, 50)]; // set the x,y,width and height based on your specs
        UIImage *buttonImage = [UIImage imageNamed:@"white.jpg"];
        [self.hardButton setImage:buttonImage forState:UIControlStateNormal];
        [self.view addSubview:self.hardButton];
    }
    

    where hardButton is a reference to your button declared in .h (for example) like

    @property (nonatomic, assign) UIButton* hardButton;
    

    Synthesize it also (if you want, if not Xcode will take care for you) like this

    @synthesize hardButton = _hardButton;
    

    If you use strong or retain instead of assign and you don't use ARC release in dealloc like

    - (void)dealloc
    { 
       [_hardButton release];
       [super dealloc];
    }
    

    Now, in your switch value changed.

    - (IBAction)switchValueChanged
    {
        UIImage* buttonImage = nil;
        if (Hard1ON.on) {
            buttonImage = [UIImage imageNamed:@"red.jpg"];
        } else {
            buttonImage = [UIImage imageNamed:@"white.jpg"];
        }
    
        [self.hardButton setImage:buttonImage forState:UIControlStateNormal];
    }