-4

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
sirwhirlwind
  • 1
  • 1
  • 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 Answers2

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
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