Iphone 调用webservice时显示警报
我有以下代码Iphone 调用webservice时显示警报,iphone,alert,Iphone,Alert,我有以下代码 UIActivityIndicator *activity = [[UIActivityIndicator alloc] initWithActivityIndicatorStyle: UIActivityIndicatorStyleWhite]; UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Process
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]需要动画,因为服务控制器调用在主线程上进行,主线程正忙于执行服务调用,阻止了要执行的警报视图动画
您需要在后端线程上执行ServiceCall,请参阅NSOperation或PerformSelectorOnBackgroundThread,确保将具有AlertView的ViewController的委托传递给后端线程,并在服务调用完成后立即回调该委托。确保使用performSelectorOnMainThread在主线程上执行回调调用。所有与UI相关的调用都应该在主线程上执行。除了上面的文章,您必须这样编写警报:
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
}
你能给出一些使用performSelectorOnBackgroundThread方法的链接吗?