4

I have code that works but I want to make sure I am doing things properly and cleanly.

I have four versions of the same collection of views displayed on a screen. Each collection which will be used to control the volume and rate of four different sounds. The collections are linked with IBOutletCollection to 4 different NSArrays (soundView0, soundView1, soundView2, soundView3).

I used the following code to determine which volume slider is being adjusted:

-(IBAction)whichVolume:(UISlider *)sender
{
    if ([soundView0 containsObject:sender]) {
        soundIndex = 0;
    }
    else if (([soundView1 containsObject:sender]))
    {
        soundIndex = 1;
    }
else if ([soundView2 containsObject:sender])
{
    soundIndex = 2;
}
    else if ([soundView3 containsObject:sender])
{
    soundIndex = 3;
}
    //send a message to set volume of sound at index soundIndex
    NSLog(@"The soundIndex is %d", soundIndex);
    NSLog(@"The volume is %f", [sender value]);
}

Did I get this right or is there a better way to accomplish this?

ChemDev
  • 827
  • 1
  • 8
  • 23
  • 1
    This works, but instead of 4 distinct ivars, you should really make them the items of an array and use enumeration, that way your code would be much cleaner. –  Sep 21 '12 at 05:24

1 Answers1

0

You can use tag property to set an numeric index on a control, and then simply use sender.tag in event callback.

proxi
  • 1,243
  • 10
  • 18