I'm essentially brand new to developing iOS in XCode, so this is probably a really simply question. I want to know how I can change the background color of my View Controller using a simple switch- in other words, how do I make it so that when a switch is tapped, the background is no longer white? I know how to do this in the interface builder, but it's not interactive.
Asked
Active
Viewed 4,526 times
-4
-
What have you setup so far? Do you know how to sets a view's background color? Do you know how to handle a switch event? Show the relevant code you have so far. – rmaddy Sep 17 '14 at 00:04
2 Answers
0
You need to create a method to do it, such as:
- (void)changeColor:(UISwitch *)mySwitch {
if (mySwitch.isOn)
[self.view setBackgroundColor:color];
else
[self.view setBackgroundColor:[UIColor whiteColor]];
}
Then, you hook the method up to the switch like:
[mySwitch addTarget:self selector:@selector(changeColor:) forControlEvent:UIControlEventValueChanged

Jsdodgers
- 5,253
- 2
- 20
- 36
-
With the code to hook the method to the switch, where would you put that code? Sorry, I'm just barely starting. – sirwhirlwind Sep 17 '14 at 00:19
-
0
The simplest way to do this would configure your switch or button to an IBAction method. You write a method and right click and drag to this method to connect the switch.
- (IBAction)changeBackgroundColor: (id) sender {
if(sender.on) {
self.view.backgroundColor = [UIColor blueColor];
} else {
self.view.backgroundColor = [UIColor redColor];
}
}

tbreinhart
- 25
- 7