簡體   English   中英

iPhone UILabel未更新

[英]iPhone UILabel not updating

我知道周圍有許多類似的問題,但是似乎沒有一個答案可以解決我的問題。 我有一個使用NSURLConnection下載文件的應用程序,然后對下載的文件進行了一些計算。 我設置了一個UILabel來顯示當前的加載狀態(例如:“正在加載文件”,“正在解析文件”)。 我在NSURLConnection委托的didReceiveResponse和connectionDidFinishLoading函數以及代碼中的其他一些地方更新了UILabel。 我通過調用以下函數來更新它:

[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO]

其中-(void)updateProgress是我定義為調用[theLabel setNeedsDisplay]的函數。 我NSLog了它,就像

NSLog(@"theLabel: %@\n",theLabel.text);

並且信息已正確更新,但標簽實際上並未在視圖中更新。 此外,僅在所有內容加載之后才調用updateProgress。 它會更新標簽THEN,這幾乎沒有用。 有什么建議么?

NSURLConnection阻塞了主線程(在完成之前,不會在視圖上執行任何更新)。

您可以在后台執行updateProgress

[self performSelectorInBackground:@selector(updateProgress) withObject:nil]

updateProgress的第一行應為:

NSAutoReleasePool *pool = [[NSAutoReleasePool alloc]init];

最后幾行應為:

[pool release];
pool = nil;

http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html

當然,您也可以在后台執行NSURLConnection。 然后,您可以在主線程上更新標簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM