switch 语句中只有 1 种情况有效(目标 C)

发布于 2024-11-27 05:08:01 字数 1149 浏览 1 评论 0原文

我正在 Xcode 中开发合成器应用程序。我正在使用自定义滑块和旋钮。我希望它们将控制值发送到嵌入式纯数据补丁中的接收器(我使用 libpd 作为纯数据库和包装器)。

我的界面上有多个自定义滑块和旋钮。我希望每个人只发送自己的控制值,独立于其他滑块/旋钮。

我正在使用标签和 switch 语句。 问题是只有第一种情况可以工作,并且所有滑块和旋钮都将控制值发送到同一接收器。

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

然后我在这里实现该方法...

- (void)controlValueDidChange:(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;
    }     
}

有人可以帮忙吗?谢谢。

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)controlValueDidChange:(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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

闻呓 2024-12-04 05:08:01

哎呀,还在吗?

显然您需要为每个滑块分配一个单独的目标!

另外,在您的示例中,您在创建滑块之前分配标签,这不好。您应该始终在设置任何属性之前放置 init

- (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"];
}

如果您要使用我对您的回答其他问题,您至少可以投赞成票。 ;—]

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. ;—]

瘫痪情歌 2024-12-04 05:08:01

您应该在此处重新检查您的 switch 语法。基本上,您应该丢失内部分隔符。

You should re-check your switch syntax here. Basically, you should lose the inner delimiters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文