首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在调用webservice时显示警报

问在调用webservice时显示警报
EN

Stack Overflow用户
提问于 2011-03-09 15:51:40
回答 2查看 558关注 0票数 2

我有以下代码。

代码语言:javascript
复制
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中获取详细信息并返回它们。在调用服务时应显示警报,因为获取详细信息需要时间。但它仅在服务呼叫结束后显示警报。为什么会这样呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-09 16:06:37

因为警报显示将需要动画,所以由于服务控制器调用发生在主线程上,所以主线程忙于执行服务调用,从而阻塞了警报视图动画的执行。

您需要在后端线程上执行ServiceCall,请参阅NSOperation或PerformSelectorOnBackgroundThread,确保将具有AlertView的ViewController的委托传递到后端线程,服务调用完成后立即回调委托。确保使用performSelectorOnMainThread在主线程上执行回调。所有与UI相关的调用都应该在主线程上执行。

票数 3
EN

Stack Overflow用户

发布于 2011-03-09 17:56:22

除了上面的帖子,你必须这样写警报:

代码语言:javascript
复制
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服务调用,如下所示:

代码语言:javascript
复制
-(void)doTheWork {

WebServiceController *web = [[WebServiceController alloc]init];

NSDictionary *dict = [web getDetails];

[alert dismissWithClickedButtonIndex:0 animated:YES]; //dismissing the alert

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5242818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档