2012-02-20 11 views
0

私はObjCを初めて使用しています。私は4つのボタン(すでにストーリーボードにリンク)のそれぞれのタグをチェックforループを書くことで方法を記入したい場合、私はどのようにするだろう、ボタンのタグを確認するループ

-(IBAction)playSound:(id)sender; 

と呼ばれる私のコントローラクラスにメソッドを持っていますそれ?ボタンをクリックして音を鳴らそうとしています。あなたの答えに記述してください。

+3

あなたは押されたボタンのタグを決定しようとしていますか? '((UIButton *)sender).tag'を使用することができます – nielsbot

+1

ボタンをクリックしてサウンドを再生するには、配列/辞書をボタンに一致させるだけです –

+0

なぜ' for'ループを使用しますか? – bneely

答えて

2
-(IBAction)playSound:(id)sender 
{ 
    UIButton *button = (UIButton *)sender; // this is the button that has been pressed 
    if (button.tag == 0) { 
     // Play song for button 0 
    } else if (button.tag == 1) { 
     // Play song for button 1 
    } // ... 
} 

それとも

-(IBAction)playSound:(id)sender 
{ 
    UIButton *button = (UIButton *)sender; // this is the button that has been pressed 
    NSString *songName = [songs objectAtIndex:button.tag]; // songs is an array 
    // Play song 
} 
関連する問題