我有一个按钮,该按钮向网络服务发送调用并返回一些值。我想显示一个没有按钮的警告框,直到值从 web 服务返回。但是 alertWait 在收到值后显示。我尝试使用设置警报功能并从按钮的开头调用,但它也显示在接收值的末尾。我哪里出错了这是我的代码。
- (IBAction)UploadButton:(id)sender
{
// Initializtion of some elements
UIAlertView *alertWait;
alertWait = [[UIAlertView alloc] initWithTitle:@"Please Wait..."
message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[alertWait show];
//Web service call statements
if ([status intValue] == 0)
{
NSLog(@"It is zero");
[alertWait dismissWithClickedButtonIndex:0 animated:YES];
UIAlertView *alert =[ [UIAlertView alloc]initWithTitle:@"Success" message:@"You have successfully uploded the document and waiting for approval" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
最佳答案
将服务调用放在后台线程上,就像下面更改的示例代码一样:
- (IBAction)UploadButton:(id)sender
{
// Initializtion of some elements
UIAlertView *alertWait;
alertWait = [[UIAlertView alloc] initWithTitle:@"Please Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[alertWait show];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
//Web service call statements
dispatch_async(dispatch_get_main_queue(), ^{
//Do your UI operations here
if ([status intValue] == 0)
{
NSLog(@"It is zero");
[alertWait dismissWithClickedButtonIndex:0 animated:YES];
UIAlertView *alert =[ [UIAlertView alloc]initWithTitle:@"Success" message:@"You have
successfully uploded the document and waiting for approval" delegate:self
cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
});
});
}
关于ios - 警报 View 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23843752/