我一直在为这个问题撞墙,需要一些帮助。
我试图在后台加载数据时显示 UIActivityIndicator。我不确定这是否相关,但我正在加载一个表格 View 。指示器出现,但不旋转……除非我触摸屏幕,或者在加载时发生其他事情,比如我收到一条短信。代码如下:
UIActivityIndicatorView *av = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
av.frame=CGRectMake(145, 160, 25, 25);
av.tag = 1;
[self.mTableView addSubview:av];
[av startAnimating];
[self performSelectorInBackgroundselector(load) withObject:nil];
我也试过这个:
UIActivityIndicatorView *av = [[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
av.frame=CGRectMake(145, 160, 25, 25);
av.tag = 1;
[self.view addSubview:av];
[av startAnimating];
[self performSelectorInBackgroundselector(load) withObject:nil];
并尝试注释掉最后一行 - 所以 not 让后台线程运行。我已经在 viewDidLoad 和 viewDidAppear 方法中尝试了这两个版本的代码。
有什么想法吗?
编辑这是我的加载方法
- (void)load {
NSString *post = [NSString stringWithFormat"id[]=%@", [ids objectAtIndex:0]];
for(int i = 1; i < ids.count; i++){
post = [NSString stringWithFormat"%@&id[]=%@", post, [ids objectAtIndex:i]];
}
NSURL *url=[NSURL URLWithString"http://myurl/instructions"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod"OST"];
[request setValue:postLength forHTTPHeaderField"Content-Length"];
[request setValue"application/x-www-form-urlencoded" forHTTPHeaderField"Content-Type"];
[request setHTTPBody:postData];
/* when we user https, we need to allow any HTTPS cerificates, so add the one line code,to tell teh NSURLRequest to accept any https certificate, i'm not sure about the security aspects
*/
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
[self parseData:data];
[spinner stopAnimating];
}
Best Answer-推荐答案 strong>
您没有包含足够的代码来查看停止 UIActivityIndicator 动画的位置以及显示动画时发生的情况,但问题几乎可以肯定是以下问题之一:
1) 您希望您的代码等待某个异步方法,这会导致您的事件指示器过早关闭,
或
2) “后台”任务和 UIActivityIndicator 都在同一个线程上运行,而您的“后台”任务将线程独占到事件指示器不够用的地步是时候制作动画了。
这篇文章提供了一个如何将 UIActivityIndicator 推送到自己的线程中的示例:
Activity Indicator doesn't spin
关于iphone - UIActivityIndicator 没有动画,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/10079483/
|