0

我正在尝试UILabel在“while 循环”中更新 a,但它不会更改UILabel文本。我知道它像往常一样在主线程的当前运行循环周期结束时显示iOS。但是如何解决这个问题( ftp.AsyncFinished 函数由外部 chilkat ftp 模块提供):

数据每秒更新一次。我搜索了这个论坛和 Apple 的文档,但我无法找出UILabel在主线程的“while 循环”中更新 a 的正确方法。我可以使用什么来代替 while 循环,它允许主线程更新UILabel.

while (ftp.AsyncFinished != YES) {
    // here is the code getting data from the cilkat FTP module stored in PercentOfFile and MinutesRemaining
    NSString *content =[[NSString alloc] initWithFormat:@"%.0f%% done%.0fmin left",PercentOfFile,MinutesRemaining ];
    [LabelProgress setText:content];
}
4

4 回答 4

4

为了让 UI 完全更新,主线程的运行循环必须完成。这意味着您的方法(如果在主线程上调用)必须在任何 UI 更新发生之前返回。然后合并所有更新并重绘 UI。

请参阅强制 UIView 重绘的最可靠方法是什么?了解更多背景信息。

您的while循环被错误地设计为异步更新。您不应该在while循环中间获取异步数据。您应该注册某种回调(通常将自己设置为delegate),然后等待系统呼叫您。

于 2012-05-02T13:04:36.340 回答
1

您需要在另一个线程中调用您的上传进度并注册回调。创建一个继承自 CkoFTP2Progress 的自定义类,该类具有以下方法:

- (void)PercentDone: (NSNumber *)pctDone abort:(BOOL *)abort;

假设您有一个名为“ftp”的 CkoFTp2 对象和名为“UploadProgress”的自定义类,请注册回调:

[self.ftp setEventCallbackObject:self.uploadProgress];
于 2012-12-17T20:53:48.367 回答
0

您可以将 while 循环移动到后台线程并使用 performSelectorOnMainThread 从那里调用 setText。

于 2012-05-02T09:38:26.433 回答
0
// update your UILabel here
// ...

// Delay execution of long-running task so that update of UILabel can be performed
dispatch_after( dispatch_time( DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC ), dispatch_get_main_queue(), ^{
     // long-running task 
     // ..
});
于 2015-04-27T10:11:21.547 回答