0

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.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Louis Holley
  • 135
  • 1
  • 3
  • 10
  • I added an edit for you. Hope it helps. – Lorenzo B Feb 24 '13 at 15:57
  • @flexaddicted thanks so much, one more question, how do I move this button to a different place on the screen? – Louis Holley Feb 24 '13 at 16:34
  • You need to play with `[self.hardButton setFrame:CGRectMake(0, 0, 200, 50)];` where `CGRectMake(x, y, width, height);`. Hope that helps. See also my answer on this: http://stackoverflow.com/questions/5361369/ios-programming-frame-bounds-and-center/11282765#11282765 – Lorenzo B Feb 24 '13 at 16:38

3 Answers3

4

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];
}
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • Thanks for the help, however when I try to run the program after making that change, [this](http://gyazo.com/0b8c617581450689654ff7a14456752b.png?1361707862) appears. And about the camelCase notation - I usually do that, however the look of hard1 is just displeasing to me haha. hardOne, for instance, looks ok to me. I'm just in the very basic stages of this program so I'll start to clean it up, but I guess it's better to keep it tidy from the start. Thanks again for the advice. – Louis Holley Feb 24 '13 at 12:11
  • Please provide details on your question. Do not provide external links. – Lorenzo B Feb 24 '13 at 12:12
  • StackOverflow has a built in image sharing space. – Lorenzo B Feb 24 '13 at 12:14
  • Sorry about that, I didn't realise. It highlights a part of my AppDelegate in green and says 'Thread 1: signal SIGABRT'. Also - what part of my question would you like more detail @flexaddicted? – Louis Holley Feb 24 '13 at 12:16
  • @LouisHolley I mean you should upload the image in your question, if possible. In this way, other people can see the problem and suggest a fix. Thank you very much. – Lorenzo B Feb 24 '13 at 12:18
  • You got an exception. See http://stackoverflow.com/questions/4961770/run-stop-on-objective-c-exception-in-xcode-4 for enabling exceptions in Xcode. – Lorenzo B Feb 24 '13 at 12:23
  • I don't have 10 rep and I need that to post an image unfortunately. If I enable the exception it still stops the program, it just changes the green message from 'signal SIGABRT' to 'exception breakpoint' – Louis Holley Feb 24 '13 at 12:38
  • @LouisHolley Could you share the code you use to create the button? thanks – Lorenzo B Feb 24 '13 at 14:50
  • I used `UIButton *Hard1;` and `@property (nonatomic, retain) IBOutlet UIButton *Hard1;` in the interface file, then ctrl, clicked and dragged the Outlet to my button in the Interface Builder. Thanks for the response again. – Louis Holley Feb 24 '13 at 15:18
  • In this case, using a xib, you don't need to add the button in `viewDidLoad` method. It should works as is. Could you provide the crash log you see in the console? Please, edit your question with further details. – Lorenzo B Feb 24 '13 at 15:26
  • There is no crash log, the program carries out fine, the colour of the button simply remains unchanged – Louis Holley Feb 24 '13 at 15:32
0

I think you want to use -setBackgroundImage:forState: instead of -setImage:forState:

And also, as @flexaddicted said, you only want to add the button once.

iain
  • 5,660
  • 1
  • 30
  • 51
0

If you are using button as switch you can like this way.

  _btnMale = [UIButton buttonWithType:UIButtonTypeCustom];
   [_btnMale setBackgroundImage:[ UIImage imageNamed:@"checkBox.png"] forState:UIControlStateNormal];
   [_btnMale setBackgroundImage:[ UIImage imageNamed:@"checkMark.png"] forState:UIControlStateSelected];
   _btnMale.tag = 1;
   _btnMale.frame = CGRectMake(150 , 9, 25, 25);
   [_btnMale addTarget:self action:@selector(selectGender:) forControlEvents:UIControlEventTouchUpInside];
   [self addSubview:_btnMale];

Set button image for selected and normal state in viewDidLoad/viewDidAppear method as per your need. After declaration, in your target method add this code.

-(void)selectGender:(UIButton*)sender{

  sender.selected = ! sender.selected;

}

And then set other button to selected NO. then button will work like as a switch.

hope this will help you.

Nirav Jain
  • 5,088
  • 5
  • 40
  • 61