0

I had luck with the below code using a UISwitch to toggle. I need a UIButton to do the same. Not sure what I need to change to make that work? Thanks for any help!

- (IBAction)switchNotes:(id)sender {
    if([sender isOn]){
        NSLog(@"Switch is ON");
        for(UIButton *myButton in self.myButtonCollection) {
            [myButton setTitleColor:[BT_color getColorFromHexString:@"#FFFF00"] forState:UIControlStateNormal];
            [myButton setAlpha:1.0f];
        }
    } else{
         NSLog(@"Switch is OFF");
        for(UIButton *myButton in self.myButtonCollection) {
            [myButton setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
            [myButton setAlpha:1.0f];

        } 
    } 
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Ian James
  • 61
  • 7

2 Answers2

1
- (IBAction)switchNotes:(id)sender
{
    sender.selected = !sender.selected;
    if(sender.selected)
    {
        NSLog(@"Switch is ON");
        for(UIButton *myButton in self.myButtonCollection) {
            [myButton setTitleColor:[BT_color getColorFromHexString:@"#FFFF00"] forState:UIControlStateNormal];
            [myButton setAlpha:1.0f];
        }
    }
    else
    {
        NSLog(@"Switch is OFF");
        for(UIButton *myButton in self.myButtonCollection) {
            [myButton setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
            [myButton setAlpha:1.0f];
        }
    }
}
dstudeba
  • 8,878
  • 3
  • 32
  • 41
NIMO
  • 33
  • 1
  • Hi NIMO, thanks for the code! I'm guessing I'll need to set some button attributes in xib interface builder? can you guide me with that? – Ian James Jun 10 '15 at 14:08
0

I believe your problem is that you are using the isOn property which is a property of UISwitch. According to this link iPhone UIButton with UISwitch functionality you should use the selected property of UIButton.

Community
  • 1
  • 1
dstudeba
  • 8,878
  • 3
  • 32
  • 41