Ios UiButton作为开关,然后有其他按钮跟随

Ios UiButton作为开关,然后有其他按钮跟随,ios,button,switch-statement,Ios,Button,Switch Statement,有麻烦了。 我有一个UIButton,我想作为一个开/关开关来告诉其他按钮运行一些代码。 基本上,如果UIButton是“开”的,那么点击屏幕上的所有其他按钮将随机更改视图的背景色。 谢谢你的帮助 这就是我的想法。超级新的,不太明白。谢谢你的帮助 // Buttons Play Sounds //////////////////////////////////////////////////////// - (IBAction)D1{ [self playD1]; } - (IBA

有麻烦了。 我有一个UIButton,我想作为一个开/关开关来告诉其他按钮运行一些代码。 基本上,如果UIButton是“开”的,那么点击屏幕上的所有其他按钮将随机更改视图的背景色。 谢谢你的帮助

这就是我的想法。超级新的,不太明白。谢谢你的帮助

// Buttons Play Sounds
////////////////////////////////////////////////////////

- (IBAction)D1{
    [self playD1];

}

- (IBAction)A2{
    [self playA2];
}

- (IBAction)Bb3{
    [self playBb3];
}

- (IBAction)D5{
    [self playD5];
}

- (IBAction)G7{
    [self playG7];


// Make array of the buttons?
- (IBAction)switchNotes:(UIButton *)sender {


    sender.selected = !sender.selected;
    if(sender.selected)
    {
        NSLog(@"Switch is ON");
        for(UIButton *myButton in self.myButtonCollection) {
            // What to do?


        }
    }
    else
    {
        NSLog(@"Switch is OFF");
        for(UIButton *myButton in self.myButtonCollection) {
           // what to do? 
        }

    }

您可以在随机颜色生成器中实现一个
if()
语句测试
youBut.selected
或另一个存储它的变量,如:

-(void)changeBackground:(UIButton*)sender{
if(!but.selected) return;
int r = rand() % 255;  // or implement special method to treat range with sender.tag
int g = rand() % 255;  
int b = rand() % 255;  
myView.backgroundColor = [UIColor colorWithRed:r Green:g Blue:b];

}

请发布您正在进行的工作实现。当按钮处于关闭位置时,应该是什么行为?嗨,伙计们,我在上面添加了代码。谢谢你的帮助!Hi BeemerFan在关闭位置时,视图背景色保持原来的黑色。打开时,屏幕上的所有按钮都可以更改背景颜色谢谢管理员!那么,这将如何与正在点击的一个声音按钮相联系呢?非常感谢@IanJames只需在iBaction中调用此函数,如下所示:
[self-changeBackground:sender]
。请注意,您可以删除
changeBackground:
中的参数,或者在按钮中实现标记,并修改
play…
方法,以便了解发送者,并将其传递给
changeBackground:
方法