0

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

SSteve
  • 10,550
  • 5
  • 46
  • 72

1 Answers1

1

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];
Jacob Jennings
  • 2,796
  • 1
  • 24
  • 26
  • 1
    I had already tried with background image. With further digging, I realised that I was trying to do this in the wrong thread. A High priority thread was in place for parsing the midi and, as I understand, UI work needs to be done in the main thread. I solved the problem by moving the UIButton messages to a void function and using 'performSelectorOnMainThread' to link to that function. Also, on further code prodding and much reading last night, I realised that I really should be learning and building my UI without Interface Builder, as I will eventually be needing many images/buttons/sliders th – Leigh Hunt Sep 14 '11 at 11:23