Only 1 case is working in switch statement (Objective C)
I am working in Xcode on a synthesizer application. I am using custom sliders and knobs. I want these to send control values to receivers in an embedded Pure Data patch (I am using libpd as a Pure Data library and wrapper).
I have multiple custom sliders and knobs on my interface. I want each one to send only their own control values independently of the other sliders/knobs.
I am using tags and a switch statement. The problem is that only the first case is working and all of the sliders and knobs are sending control values to the same receiver.
- (void)viewDidLoad {
[super viewDidLoad];
// Slider 1
slider.tag = 0;
slider = [[[DCSlider alloc] initWithDelegate:self] autorelease];
slider.frame = CGRectMake(0,0,20,100);
[self.sliderContainer addSubview: slider];
// Slider 2
slider2.tag = 1;
slider2 = [[[DCSlider alloc] initWithDelegate:self] autorelease];
slider2.frame = CGRectMake(0,0,20,100);
[self.sliderContainer2 addSubview: slider2];
}
and then I implement the method here...
- (void)controlVal开发者_运维百科ueDidChange:(float)value sender:(id)sender {
DCSlider *slidertag = (DCSlider *)sender;
switch (slidertag.tag)
{
case 0:
{
[PdBase sendFloat:value toReceiver:@"beatvol"];
}
break;
case 1:
{
[PdBase sendFloat:value toReceiver:@"bassvol"];
}
break;
}
}
Can anyone help please? Thanks.
Geez, still at it?
You obviously need to assign a separate target for each slider!
Also, in your example, you assign the tag before you create the slider, no good. You should always place your init
before setting any properties.
- (void)viewDidLoad
{
[super viewDidLoad];
// Slider 1
slider = [[[DCSlider alloc] initWithDelegate:self] autorelease];
slider.tag = 0;
slider.frame = CGRectMake(0,0,20,100);
[slider addTarget:self action:@selector(controlBassValueDidChange:) forControlEvents:UIControlEventValueChanged];
[self.sliderContainer addSubview: slider];
// Slider 2
slider2 = [[[DCSlider alloc] initWithDelegate:self] autorelease];
slider2.tag = 1;
slider2.frame = CGRectMake(0,0,20,100);
[slider2 addTarget:self action:@selector(controlBeatValueDidChange:) forControlEvents:UIControlEventValueChanged];
[self.sliderContainer2 addSubview: slider2];
}
- (void)controlBassValueDidChange:(float)value sender:(id)sender
{
[PdBase sendFloat:value toReceiver:@"bassvol"];
}
- (void)controlBeatValueDidChange:(float)value sender:(id)sender
{
[PdBase sendFloat:value toReceiver:@"beatvol"];
}
And if you are going to use my answer to your other question, the least you could do is give it an upvote. ;—]
You should re-check your switch syntax here. Basically, you should lose the inner delimiters.
精彩评论