iphone - 调用网络服务时显示警报

标签 iphone alert

我有以下代码。

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 获取详细信息并返回它们。应在调用服务时显示警报,因为获取详细信息需要时间。但它仅在服务调用结束后才显示警报。这是为什么?

最佳答案

因为[alert show]需要动画,由于服务 Controller 调用是在主线程上进行的,所以主线程正忙于执行服务调用,阻塞了alert view动画的执行。

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

关于iphone - 调用网络服务时显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5242818/

相关文章:

iphone - 新加载 View 中的导航 Controller

iphone - 我是否必须检查 LSRequiresIPhoneOS 才能查明相机是否可用?

sharepoint - 如何在更新代码中的项目时暂时禁用电子邮件通知?

javascript - 为什么这个 alert() 调用有效?

objective-c - objective-c - 为什么在 iphone 设置 : "Darken Colors" is turned on 时警报和操作表的按钮文本消失

iphone - Cocoa Touch 中的线条平滑

ios - 获取相册列表的简单示例?

iPhone模拟器无法启动?

jquery - 重新定位 jQuery 警报

react-native - 在 React Native Alert 中显示超链接?