使用UiButton作为切换开关时遇到问题

时间:2015-06-10 03:40:56

标签: ios uibutton toggle

我很幸运使用UISwitch切换以下代码。 我需要一个UIButton来做同样的事情。不知道我需要改变什么才能使它工作? 谢谢你的帮助!

- (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];

        } 
    } 
}

2 个答案:

答案 0 :(得分: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];
        }
    }
}

答案 1 :(得分:0)

我相信您的问题是您使用的是isOn属性,这是UISwitch的属性。根据此链接iPhone UIButton with UISwitch functionality,您应该使用UIButton的选定属性。