1

我正在尝试使用 Chilkat 库从我的 ftp 服务器获取目录文件列表。在这种情况下,我想在进程运行时为 UIActivityIndi​​catorView 设置动画。但问题是, UIActivityIndi​​catorView 永远不会开始动画。我使用的代码是:

[self.activityIndicator startAnimating];
[selfgetListFromPath:ftpPath withConnection:ftpConnect];
[self.activityIndicator stopAnimating];

activityIndi​​cator 是 的一个对象UIActivityIndicatorViewftpPath是我在 FTP 服务器中的文件路径的 NSString,getListFromPath是使用 Chilkat 算法从 FTP 服务器获取列表的方法,ftpConnect是 FTP 连接类的对象。

我在调用函数之前尝试使用 NSRunLoop getListFromPath,所以我将代码更改为:

[self.activityIndicator startAnimating];

BOOL waitingOnProcessing = YES;
NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
while (waitingOnProcessing && [currentRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) {

}

[self getListFromPath:ftpPath withConnection:ftpConnect];
[self.activityIndicator stopAnimating];

这使activityIndicator动画,但从getListFromPath未被解雇。试用后,我选择再次将我的代码更改为:

[self.activityIndicator startAnimating];

    BOOL waitingOnProcessing = YES;
    NSRunLoop *currentRunLoop = [NSRunLoop currentRunLoop];
    while (waitingOnProcessing && [currentRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) {
        waitingOnProcessing = NO;       
    }

    [self getListFromPath:ftpPath withConnection:ftpConnect];
    [self.activityIndicator stopAnimating];

它制作activityIndicator动画,并触发了该getListFromPath功能。但我怀疑这段代码,我对这段代码是否正确?或者也许使用 NSRunLoop 有不好的做法?谁能告诉我

谢谢

4

2 回答 2

0

我不知道 Chilkat 库,但它必须有某种方式告诉您您从 ftp 服务器收到了答复。当你得到它时,你应该使用 NSNotification 或协议来告诉你的视图控制器你得到了答案。发生这种情况时,您会停止微调器。

于 2011-07-12T09:38:44.807 回答
0

此代码在“黑匣子”中运行:

[self.activityIndicator startAnimating];
[self getListFromPath:ftpPath];
[self.activityIndicator stopAnimating];

因此,当您的方法返回时会发生 UI 更新,并且您不会看到更新。

您需要做的是 startAnimating 您的活动指示器,然后getListFromPath在另一个线程中启动您的。当该方法终止时,您回调您的主线程,告诉它停止为指示器设置动画。

使用这种方法:

[NSObject performSelectorInBackground:withObject:]

启动你的 getListFromPath 线程,然后当它完成后,调用

[NSObject performSelectorOnMainThread:withObject:waitUntilDone:]

将控制权交还给主线程,这将停止微调器动画。

于 2011-07-12T09:42:26.300 回答