Search code examples
xcodemultithreadingipadslidersetvalue

UISlider setValue not working


I've been trying for all the afternoon to properly set a value on a slider. This is driving me crazy.

First, this part of the code is working. I get a message from a webSocket (socketRocket library) that I convert to float and I set the slider to the appropriate position

- (void) webSocket:(SRWebSocket *) webSocket didReceiveMessage:(id)message{
        float stateValue = [message floatValue];
        zoneSlider.value = stateValue;
        zoneSlider.alpha = 1;
    }

So this code properly sets the value of the slider (zoneSlider) and makes it visible, just what I needed.

But for the first opening of the view I also need to set the slider programmatically to a default value (for some reasons setting it up directly in the storyboard attribute inspector is not suited to my need).

I wrote this code:

float defaultValue = 30.0f;
[zoneSlider setValue:defaultValue animated:NO];
zoneSlider.alpha = 1;

And it sets the slider to zero (top left). I've tried multiple solutions (make a float from a NSString to copy what's working with the webSocket message, using zoneSlider.value = defaultValue etc...).

Am I doing something wrong ?

Thanks !

PS: Both slider setValue (the working and non working one) are used when a message is received by the webSocket (method didReceiveMessage). Nevertheless, for the non working one the message is sent to the socket inside a dispatch_async method (that I'm absolutely not familiar with...). It might be a source of trouble though I don't see any reason for it.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    while (viewActive == true){
        // Pause between refresh
        NSDate *future = [NSDate dateWithTimeIntervalSinceNow: 5 ];
        [NSThread sleepUntilDate:future];

        //Code in this part is run on a background thread
        [self sendGetState]; //Then a message is sent and we go to the setValue
    }
});

Solution

  • I found my problem ! I made a mistake in a quite long and complicated series of "if" tests. One should have been "else if".

    Rookie mistake, I'll be more rigorous in my indentation in the future ...