Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 调用webservice时显示警报_Iphone_Alert - Fatal编程技术网

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方法的链接吗?