【问题标题】:UiButton as switch then have other buttons followUiButton 作为开关然后有其他按钮跟随
【发布时间】:2015-06-19 16:39:33
【问题描述】:

遇到了麻烦。 我有一个 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? 
        }

    }

【问题讨论】:

  • 请发布您正在进行的实施。
  • 按钮处于关闭位置时的行为应该是什么?
  • 大家好,我在上面添加了代码。感谢您的帮助!
  • 嗨 BeemerFan 在关闭位置,视图背景颜色保持原始黑色。当屏幕上的所有按钮都可以改变背景颜色时

标签: ios button switch-statement


【解决方案1】:

您可以在随机颜色生成器中实现 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];

}

【讨论】:

  • 感谢管理员!那么这将如何与被点击的声音按钮之一联系起来呢?非常感谢!
  • @IanJames 只需在您的 IBActions 中调用此函数,如下所示:[self changeBackground:sender]。请注意,您可以删除changeBackground: 中的参数或在按钮中实现标记并修改您的play... 方法,以便您知道发件人并将其传递给changeBackground: 方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-24
  • 1970-01-01
  • 1970-01-01
  • 2017-11-15
  • 1970-01-01
相关资源
最近更新 更多