在模态视图中显示警报
我有一个显示模式视图的应用程序。在最后一个模态视图中,我有一个表单。单击“完成”按钮后,将调用一个 Web 服务来传递用户输入的值。收到响应后,模态视图将被关闭。我希望显示一个警报或操作表,要求用户等待,因为 Web 服务调用需要花费大量时间。问题是警报或操作表仅在视图关闭后才会显示。为什么会发生这种情况?这是完成函数的代码:
-(void)reg:(id)sender {
if([password length] == 0) {
//show alert
}
//other validation
//This is were I write the 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];
//webservice called
//getting the response
//dismissing alert here
[self dismissModalViewControllerAnimated:YES];
}
I have an application which displays modal views. In the last modal view, I have a form. Upon clicking the done button, a web service is called which passes the values input from the user. After a response is received, the modal view is dismissed. I would like do display an alert or action sheet asking the user to wait as the web service call takes a lot of time. The problem is that the alert or action sheet gets displayed only after the view is dismissed. Why is this happening? Here is the code for the done function:
-(void)reg:(id)sender {
if([password length] == 0) {
//show alert
}
//other validation
//This is were I write the 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];
//webservice called
//getting the response
//dismissing alert here
[self dismissModalViewControllerAnimated:YES];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须为 Web 服务响应编写侦听器或通知。如果您使用 NSUrlConnection,请使用其委托来获取响应并从委托方法中关闭模态视图。在您使用的方法中,视图在调用 Web 服务后立即消失
You have to write a listener or notification for the webservice response. If you are using NSUrlConnection, use its delegate to get the response and dismiss the modalview from the delegate method. In the method you are using, the view is dismissing right after the call to the webservice
我得到了它。这不是模态视图的问题。由于主线程正在执行 Web 服务,警报被阻止。 Web 服务执行需要在后台运行。这是类似问题的链接及其答案。
调用 Web 服务时显示提醒
I got it. It wasn't a problem with modal views. The alert gets blocked because the main thread is executing the web service. The web service execution needs to be run in the background. Here's the link to a similar question with the answer.
Showing alert while calling webservice