【问题标题】:Trouble using UiButton as toggle switch使用 UiButton 作为切换开关时遇到问题
【发布时间】:2015-08-25 03:15:27
【问题描述】:

下面的代码使用 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];

        } 
    } 
}

【问题讨论】:

    标签: ios uibutton toggle


    【解决方案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];
            }
        }
    }
    

    【讨论】:

    • 您好 NIMO,感谢您的代码!我猜我需要在 xib 界面生成器中设置一些按钮属性?你能指导我吗?
    【解决方案2】:

    我相信您的问题是您正在使用 isOn 属性,它是 UISwitch 的一个属性。根据此链接iPhone UIButton with UISwitch functionality,您应该使用 UIButton 的 selected 属性。

    【讨论】:

      猜你喜欢
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      相关资源
      最近更新 更多