2015-06-19 14 views
-1

遇到麻煩。 我有一個UIButton我想作爲開/關開關告訴其他按鈕運行一些代碼。 基本上,如果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? 
     } 

    } 
+0

請發表您的工作正在進行中實現。 – JAL

+0

按鈕處於關閉位置時應該發生什麼行爲? –

+0

嗨,我添加了上面的代碼。謝謝你的幫助! –

回答

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

} 
+0

感謝管理員!那麼這將如何綁定到一個正在點擊的聲音按鈕?萬分感謝! –

+0

@IanJames簡單地在你的IBActions中調用這個函數,就像這樣:'[self changeBackground:sender]'。請注意,您可以刪除'changeBackground:'中的參數或者在按鈕中實現標籤並修改您的'play ...'方法,以便您知道發件人並將其傳遞給'changeBackground:'方法 –