leesuh
leesuh

Reputation: 29

Loop to check for the tag of a button

I am new to ObjC. I have a method in my controller class called

-(IBAction)playSound:(id)sender;

If I wanted to fill out the method by writing a for loop that checks the tag of each of my four buttons (already linked in storyboard), how would I do that? I am trying to make a button click play a sound. Please be descriptive in your answer.

Upvotes: 0

Views: 125

Answers (1)

sch
sch

Reputation: 27506

-(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
    }   // ...
}

Or

-(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
}

Upvotes: 2

Related Questions