UIButton setImage 未从非触摸操作更新

发布于 2024-12-04 12:00:39 字数 2222 浏览 4 评论 0原文

作为 obj-c 的菜鸟,我已经在这个问题上停留了一两天了。如果有人可以提供帮助,我们将不胜感激。

我有一个 ipad 应用程序,我正在使用可以更改图像的按钮(例如“静音开关”按钮)来表示和控制我的笔记本电脑上的应用程序。

我有一个 IBAction 可以触发一条 MIDI 消息通过网络发送到我的笔记本电脑 - 在这里一切正常。

UIButton 的更新发生在我的笔记本电脑上的应用程序通过 midi 消息返回时,而不是在这个 void 函数中的 touchDown 的 IBAction 中;

    - (void) midiSource:(PGMidiSource*)midi midiReceived:(const MIDIPacketList *)packetList
    {
        int firstByte = 0;
        int secondByte = 0;
        int controllerNumber = 0;
        int controllerValue = 0;

        const MIDIPacket *packet = &packetList->packet[0];
        for (int i = 0; i < packetList->numPackets; i++) {
            for (int j = 0; j < packet->length; j++) {
                if (secondByte) {
                    controllerValue = packet->data[j];
                    secondByte = 0;
                    firstByte = 0;
                    if (controllerNumber < 8) {
                        if (!controllerValue) {
                            [muteButtonIds[controllerNumber] setImage:imageMuted forState:UIControlStateNormal];
                            [muteButtonIds[controllerNumber] setImage:imageMuted forState:UIControlStateSelected];
                        }
                        else {
                            [muteButtonIds[controllerNumber] setImage:imageUnmuted forState:UIControlStateNormal];
                            [muteButtonIds[controllerNumber] setImage:imageUnmuted forState:UIControlStateSelected];
                        }
                    }
                }
                if (firstByte) {
                    secondByte = 1;
                    controllerNumber = packet->data[j];
                }
                if (packet->data[j]== 176) {
                    firstByte = 1;
                }
            }
            packet = MIDIPacketNext(packet);
        }
    }

当触摸我的 ipad UIButton 时,一切正常,没有问题。 我的问题是,当我从我的笔记本电脑应用程序发送相同的 midi 消息时,我的 ipad 上的 gui 不会更新,直到我触摸应该更改的特定按钮。

我尝试查看 setNeedsDisplay,但这只会引发 autoreleasepool 警告。

我确信这是我所缺少的简单的东西。

有人可以帮忙吗?

我来自 C 背景,MaxMSP 的编程 guis,jbox_redraw 是前进的方向(我知道这不是我应该寻找答案的地方,但这是我正在寻找的功能)通过 midi 消息

谢谢你有时间看这个。

问候, 利

As a noobie to obj-c, I've been stuck on this issue for a day or two. If anyone could help it would be greatly appreciated.

I have an ipad app I am working on with buttons that change images (think 'mute on off' button) to represent and control an app on my laptop.

I have an IBAction that triggers a midi message to be sent to my laptop via network - all works well here.

The updating of the UIButton happens on return from my app on the laptop, via a midi message, not in the IBAction for the touchDown, in this void function;

    - (void) midiSource:(PGMidiSource*)midi midiReceived:(const MIDIPacketList *)packetList
    {
        int firstByte = 0;
        int secondByte = 0;
        int controllerNumber = 0;
        int controllerValue = 0;

        const MIDIPacket *packet = &packetList->packet[0];
        for (int i = 0; i < packetList->numPackets; i++) {
            for (int j = 0; j < packet->length; j++) {
                if (secondByte) {
                    controllerValue = packet->data[j];
                    secondByte = 0;
                    firstByte = 0;
                    if (controllerNumber < 8) {
                        if (!controllerValue) {
                            [muteButtonIds[controllerNumber] setImage:imageMuted forState:UIControlStateNormal];
                            [muteButtonIds[controllerNumber] setImage:imageMuted forState:UIControlStateSelected];
                        }
                        else {
                            [muteButtonIds[controllerNumber] setImage:imageUnmuted forState:UIControlStateNormal];
                            [muteButtonIds[controllerNumber] setImage:imageUnmuted forState:UIControlStateSelected];
                        }
                    }
                }
                if (firstByte) {
                    secondByte = 1;
                    controllerNumber = packet->data[j];
                }
                if (packet->data[j]== 176) {
                    firstByte = 1;
                }
            }
            packet = MIDIPacketNext(packet);
        }
    }

All works well here without issue when touchingDown on my ipad UIButton.
My problem is, when I send the same midi message from my laptop app, the gui on my ipad is not updated until I touchDown on the specific button that should change.

I have tried looking at setNeedsDisplay, but this only throws autoreleasepool warnings.

I'm sure it is something simple I am missing.

Can anyone help?

I come from a C background, programming guis for MaxMSP, and jbox_redraw is the way forward there (I know that's not where I should be looking for answers, but that's the kind of functionality I'm looking for) via a midi message

Thanks for your time looking at this.

Regards,
Leigh

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

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

发布评论

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

评论(1

不喜欢何必死缠烂打 2024-12-11 12:00:39

首先,尝试使用 setBackgroundImage forState 而不是 setImage forState。
如果您的按钮可能会突出显示,请尝试添加额外的 setImage forState 行,以应对“正常”和“突出显示”等情况,使用按位 OR 运算符(控制状态是位掩码)

UIControlStateNormal | UIControlStateHighlighted

如果您仍然遇到问题,请尝试以编程方式选择并在为每个状态设置图像后取消选择该按钮:

[muteButtonIds[controllerNumber] setSelected:YES];
[muteButtonIds[controllerNumber] setSelected:NO];

First, try using setBackgroundImage forState instead of setImage forState.
If there is a possibility your button might be highlighted, try adding additional setImage forState lines for situations like being both "Normal" AND "Highlighted", using bitwise OR operator (the control states are bitmasks)

UIControlStateNormal | UIControlStateHighlighted

If you still have trouble, try programmatically selecting and deselecting the button after you set the images for each state:

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