将布尔值 False 放入运行循环 while
我正在尝试使用 Chilkat 库从我的 ftp 服务器获取目录文件列表。 在这种情况下,我想在进程运行时对 UIActivityIndicatorView 进行动画处理。但问题是,UIActivityIndicatorView 永远不会开始动画。我使用的代码是:
[self.activityIndicator startAnimating];
[selfgetListFromPath:ftpPath withConnection:ftpConnect];
[self.activityIndicator stopAnimating];
activityIndicator是UIActivityIndicatorView
的对象,ftpPath
是我在FTP服务器中的文件路径的NSString,getListFromPath
是使用 Chilkat 算法从 FTP 服务器获取列表的方法,ftpConnect
是 FTP 连接类的对象。
我在调用 getListFromPath 函数之前尝试使用 NSRunLoop,因此我将代码更改为:
[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 有一个不好的做法? 有人可以告诉我吗
谢谢
i'm trying to get directory file listing from my ftp server using Chilkat library.
In this case, i want to animating a UIActivityIndicatorView when the process is running. But the problem is, the UIActivityIndicatorView never start to animate. The code I use is :
[self.activityIndicator startAnimating];
[selfgetListFromPath:ftpPath withConnection:ftpConnect];
[self.activityIndicator stopAnimating];
activityIndicator is an object of UIActivityIndicatorView
, ftpPath
is a NSString of my file path in FTP server, and getListFromPath
is the method for getting list from FTP server using Chilkat algorithm, ftpConnect
is an object of FTP Connection Class.
I was try to use NSRunLoop before called getListFromPath
function, so I changed my code into :
[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];
this make the activityIndicator
animating, but the getListFromPath
never fired. After a trial, i choosed to changed again my code into :
[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];
it make the activityIndicator
animating, and also fired the getListFromPath
function. But i'm doubt with this code, am i right with this code? or maybe there is a bad practice for using NSRunLoop?
can somebody tell me
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此代码在“黑匣子”中运行:
因此当您的方法返回时会发生 UI 更新,并且您不会看到更新。
您需要做的是 startAnimating 您的活动指示器,然后在另一个线程中启动您的
getListFromPath
。当该方法终止时,您回调主线程,告诉它停止对指示器进行动画处理。使用此方法:
启动 getListFromPath 线程,然后在完成后,调用
将控制权交还给主线程,这将停止微调器动画。
This code runs as in a "black box" :
So UI updates occur when your method returns, and you won't see the update.
What you need to do is startAnimating your activity indicator, then start your
getListFromPath
in another thread. When that method terminates, you call back your main thread telling it to stopAnimating the indicator.Use this methods:
to start your getListFromPath thread, then when it's done, call
to give the control back to the main thread, which will stop the spinner animation.
我不知道 Chilkat 库,但它一定有某种方式告诉您您从 ftp 服务器收到了答案。当你得到它时,你应该使用 NSNotification 或协议来告诉你的视图控制器你得到了答案。当这种情况发生时,你停止旋转。
I dont know the Chilkat library, but it must have some way of telling you that you receive an answer from your ftp server. When you got it, you should use a NSNotification or a protocol to tell your view controller that you got an answer. When that happen you stop the spinner.