The slider's values are transferred in the socket, but the slider visual position doesn't change
I have a connection between iPod client and C server. I can send the data of the slider as I move the slider, and get the data on the server correctly. Only problem is the blue point of the slider doesn't move, nor does the label under it change the slider's value. When I disconnect, the blue point moves to the point it should be and the label shows its value. Here are the codes:
-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
switch (streamEvent)
{
case NSStreamEventHasSpaceAvailable:
event = @"NSStreamEventHasSpaceAvailable";
connectButton.enabled = NO;
disconnectButton.enabled = YES;
if (theStream == oStream)
{
//send data
const uint8_t *buffer = (const uint8_t *)[dataSlider UTF8String];
NSInteger err = [self writeToServer:buffer];
if ( err == -1)
NSLog(@"Error sending data.");
else
NSLog(@"Success sending data.");
}
break;
}
- (IBAction)sliderChanged:(id)sender
{
UISlider *slider = (UISlider *)sender;
progressAsInt = (int)(slider.value + 0.5f);
sliderValue = [[NSString alloc] initWithFormat:@"%d", progressAsInt];
sliderLabel.text = sliderValue;
dataSlider = sliderValue;
}
This is the way it should work: the server is always asking for something to printf. I move the slider and send its valu开发者_运维百科e, whenever the server calls read().
Problem: the slider doesn't visually move, but its value changes (i can see it in the server's printfs).
Any thoughts?
There might be two problems:
You need to call
setNeedsDisplay
.You are using a synchronous/blocking connection with the server. If you use a synchronous/blocking communication then everything, including UI update, will wait until the communication is over. If that is the case then use a asynchronous connection. As the UI is updated when you are disconnected, this is the most probable problem.
Hum.. i got it working, but kinda laggy :s the code used was: (in the stream:handleEvent:
method)
case NSStreamEventHasSpaceAvailable:
event = @"NSStreamEventHasSpaceAvailable";
connectButton.enabled = NO;
disconnectButton.enabled = YES;
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (theStream == oStream)
{
//send data
const uint8_t *buffer = (const uint8_t *)[dadosSlider UTF8String];
NSInteger err = [self writeToServer:buffer];
// [msg release];
if ( err == -1)
NSLog(@"Erro a enviar os dados.");
else
NSLog(@"Transmitiu com sucesso.");
}
});
break;
Ideas on how to get it working flawlessly? Thks
(Just to be clear. The aim here is for the iPod only send data at the moment the server asks for a read()
)
Seems that the constant "knowing that the stream had space available" was clogging the program and UI. I changed the flow of the program to wait for a message from the sever and only after that it would send the data. Here it is:
case NSStreamEventHasBytesAvailable:
event = @"NSStreamEventHasBytesAvailable";
if (theStream == iStream)
{
//read data
uint8_t buffer[1024];
int len;
while ([iStream hasBytesAvailable])
{
len = [iStream read:buffer maxLength:sizeof(buffer)];
if (len > 0)
{
NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
if (nil != output)
{
//Now that the server gave its signal to move on, send data to server
NSString *data = [[torqueValue stringByAppendingString:@" "] stringByAppendingString:angleValue];
const uint8_t *buffer2 = (const uint8_t *)[data UTF8String];
NSInteger err = [self writeToServer:buffer2];
if ( err == -1)
NSLog(@"Erro a enviar os dados.");
else
NSLog(@"Transmitiu com sucesso.");
}
}
}
}
break;
精彩评论