2011-03-31 38 views
1

我試圖從服務器加載數據時更新UIProgressview。我有一個具有sharedInstance的類來處理服務器通信,並且有一種從服務器獲取數據的方法(在while循環中)。現在我想通過從我的server-communication-Class(s​​haredInstance)發送通知來更新我的UIProgressView,並且它根本不起作用。更新帶有通知的UIProgressView

在InterfaceBuilder中正確設置了UIProgressView並且show/hide/setProgress工作正常,但setProgress無法通過通知工作。

下面是測試循環在我的服務器通信類:

- (void)completeServerQueue { 

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

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

    float test = [progress floatValue]; 

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

    sleep(1); 
} 

}

而且這是在檢測到通知時調用的方法(我用的斷點檢查了它,它是執行...):

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

}

任何想法?

回答

0

代碼是否在後臺線程上與服務器通話?

如果是這樣,你可能想嘗試這樣做:

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

然後,您將需要此方法:

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

你可能試圖更新從一個單獨的線程不工作的進展情況正常。嘗試以下操作。

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