我有以下代码。
UIActivityIndicator *activity = [[UIActivityIndicator alloc] initWithActivityIndicatorStyle:
UIActivityIndicatorStyleWhite];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Processing" delegate:self otherButtonTitles:nil];
[alert addSubview:activity];
[activity startAnimating];
[alert show];
WebServiceController *web = [[WebServiceController alloc]init];
NSDictionary *dict = [web getDetails];问题是没有显示警报。WebServiceController是一个XML解析器,它从指定的URL中获取详细信息并返回它们。在调用服务时应显示警报,因为获取详细信息需要时间。但它仅在服务呼叫结束后显示警报。为什么会这样呢?
发布于 2011-03-09 16:06:37
因为警报显示将需要动画,所以由于服务控制器调用发生在主线程上,所以主线程忙于执行服务调用,从而阻塞了警报视图动画的执行。
您需要在后端线程上执行ServiceCall,请参阅NSOperation或PerformSelectorOnBackgroundThread,确保将具有AlertView的ViewController的委托传递到后端线程,服务调用完成后立即回调委托。确保使用performSelectorOnMainThread在主线程上执行回调。所有与UI相关的调用都应该在主线程上执行。
发布于 2011-03-09 17:56:22
除了上面的帖子,你必须这样写警报:
UIActivityIndicator *activity = [[UIActivityIndicator alloc] initWithActivityIndicatorStyle:
UIActivityIndicatorStyleWhite];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Processing" delegate:self otherButtonTitles:nil];
[alert addSubview:activity];
[activity startAnimating];
[alert show];
[self performSelectorInBackground:@selector(doTheWork) object:nil];您需要声明一个函数(doTheWork),该函数将处理web服务调用,如下所示:
-(void)doTheWork {
WebServiceController *web = [[WebServiceController alloc]init];
NSDictionary *dict = [web getDetails];
[alert dismissWithClickedButtonIndex:0 animated:YES]; //dismissing the alert
}https://stackoverflow.com/questions/5242818
复制相似问题