开发者

UIButton setImage not updating from non touch action

开发者 https://www.devze.com 2023-04-04 09:03 出处:网络
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.

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;
                    con开发者_如何学运维trollerNumber = 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


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];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号