调用网络服务时显示警报
我有以下代码。
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 获取详细信息并返回它们。应在调用服务时显示警报,因为获取详细信息需要时间。但它仅在服务呼叫结束后才显示警报。这是为什么?
i have the following code.
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];
The problem is that the alert is not getting displayed. The WebServiceController is an XML parser which gets details from a specified URL and returns them. The alert should be shown while the service is called as it takes time to get the details. But it displays the alert only after the service call is over. Why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为[alert show]需要动画,由于服务控制器调用发生在主线程上,主线程正忙于执行服务调用,阻塞了alert view动画的执行。
您需要在后端线程上执行ServiceCall,请参阅NSOperation或PerformSelectorOnBackgroundThread,确保将具有AlertView的ViewController的委托传递给后端线程,服务调用完成后立即回调该委托。确保使用performSelectorOnMainThread 在主线程上执行回调调用。所有与 UI 相关的调用都应在主线程上执行。
because [alert show] will require animation, since the service controller call is taking place on the main thread, the main thread is busy executing the service call, blocking the alert view animation to execute.
You need to perform the ServiceCall on backend thread, see NSOperation or PerformSelectorOnBackgroundThread, make sure you pass in delegate of the ViewController that has the AlertView to the backend thread, callback the delegate as soon as the service call is completed. Make sure you perform the call for the callback on the mainthread using performSelectorOnMainThread. All UI related calls should be executed on the main thread.
添加到上面的帖子中,您必须像这样编写警报:
您需要声明一个函数 (doTheWork),它将处理 Web 服务调用,如下所示:
Adding to the above post, you have to write the alert like this:
You need to declare a function (doTheWork) which will take care of the web service call as follows: