在模态视图中显示警报

发布于 2024-10-20 23:33:31 字数 944 浏览 3 评论 0原文

我有一个显示模式视图的应用程序。在最后一个模态视图中,我有一个表单。单击“完成”按钮后,将调用一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

躲猫猫 2024-10-27 23:33:31

您必须为 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

说谎友 2024-10-27 23:33:31

我得到了它。这不是模态视图的问题。由于主线程正在执行 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文