开发者

Update UIProgressView with a Notification

开发者 https://www.devze.com 2023-02-21 19:11 出处:网络
I\'m trying to update a UIProgressview while loading data from a server. I have a Class with a sharedInstance to handle the server communication and there is a method which gets data from the server (

I'm trying to update a UIProgressview while loading data from a server. I have a Class with a sharedInstance to handle the server communication and there is a method which gets data from the server (in a while loop). Now I would like to update my UIProgressView by sending notifications from my server-communication-Class (sharedInstance), and it simply doesn't work.

The UIProgressView is correctly set in InterfaceBuilder and show/hide/setProgress works fine, but setProgress doesn't work through notification.

Here is the test-loop in my server-communication-Class:

- (void)completeServerQueue {

NSNumber *progress = [[NSNumber alloc] init];
for (int i=0; i<15; i++) {

    progress = [[NSNumber alloc] initWithFloat:(100/15*i) ];

    flo开发者_运维知识库at test = [progress floatValue];

    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"ServerQueueProgress"
     object:progress];

    sleep(1);
}

}

And this is the Method called when the notification is detected (I checked it with breakpoints, it is executed...):

- (void)serverQueueProgress:(NSNotification *)notification {
[serverQueueProgressView setProgress:[[notification object] floatValue]];

}

Any ideas?


Is the code talking to the server on a background thread?

If so you may want to try doing:

[self performSelectorOnMainThread: @selector(updateProgress:) withObject: [notification object]];

You will then need this method:

- (void) updateProgress: (CGFloat) value
{
      [serverQueueProgressView setProgress: value];
}


You are probably trying to update the progress from a separate thread which does not work properly. Try the following.

- (void)serverQueueProgress:(NSNotification *)notification {
    if(![NSThread isMainThread])
    {
        [self performSelectorOnMainThread:_cmd withObject:notification waitUntilDone:NO];
        return;
    }
    [serverQueueProgressView setProgress:[[notification object] floatValue]];
}
0

精彩评论

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

关注公众号