Method for determining sender ID

I have a code that works, but I want to make sure that I am doing everything correctly and cleanly.

I have four versions of the same set of views displayed on the screen. Each collection that will be used to control the volume and speed of four different sounds. Collections are associated with IBOutletCollection with 4 different NSArrays (soundView0, soundView1, soundView2, soundView3).

I used the following code to determine which slider control is 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]); } 

I got it right or is there a better way to do this?

+4
source share
1 answer

You can use the tag property to set the numeric index in the control, and then just use sender.tag in the event callback.

0
source

Source: https://habr.com/ru/post/1435432/


All Articles