2012-02-20 62 views
0

我是新来的ObjC。我有一个方法,我称之为循环检查按钮的标签

-(IBAction)playSound:(id)sender; 

控制器类如果我想要写一个for循环,检查我的每个四个按钮(在故事板已连接)的标签填写方法,我会怎么做那?我试图让按钮点击播放声音。请在你的回答中描述。

+3

是你要确定被按下的按钮的标签?你可以使用'((UIButton *)sender).tag' – nielsbot 2012-02-20 02:58:42

+1

如果你想要一个按钮点击来播放声音,只需制作一个数组/字典来匹配你的按钮。 – 2012-02-20 03:07:56

+0

为什么要使用'for'循环? – bneely 2012-02-20 03:20:20

回答

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 
}