iphone - UIAlertView 迟到了

标签 iphone ios web-services uialertview

我在我的 iOS 应用程序中与服务器通信。我有以下方法可以打开警报 View 。我想在应用从服务器获取响应时显示加载 View 。

- (void) showDetailedQuestion:(id)sender
{
       //loading view                  
        self.loading_alert = [[UIAlertView alloc] initWithTitle:@"Loading\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
        [self.loading_alert show];

        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

        // Adjust the indicator so it is up a few pixels from the bottom of the alert
        indicator.center = CGPointMake(loading_alert.bounds.size.width / 2, loading_alert.bounds.size.height - 50);
        [indicator startAnimating];
        [self.loading_alert addSubview:indicator];

    UIButton *btn = (UIButton*)sender;
    int indx = btn.tag;

    NSLog(@"tag:%d",indx);
    answerAnQuestion *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Answer"];

    vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal ;
    vc.que_id = [self.que_id valueForKey:[NSString stringWithFormat:@"%d",indx]];
    vc.qid_list = self.que_id;
    vc.crnt = indx;

    [self presentViewController:vc animated:YES completion:nil];
    [self.loading_alert dismissWithClickedButtonIndex:0 animated:YES];
}

在另一个 answerAnQuestion.m 中

- (void)viewDidLoad
{
            NSString *address = [NSString stringWithFormat:@"%@%@%@%@%@%@%@", path,@"questions/",que_id,@"?token=",token,@"&user_id=",usrId];
    NSURL *URL = [NSURL URLWithString:address];
    NSLog(@"%@",address);

    [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[URL host]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
                                                       timeoutInterval:60.0];
    [request setHTTPMethod:@"GET"];
    responseData = [[NSMutableData alloc] init];

    NSURLResponse *response = nil;
    NSError *error = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

    if (data) 
    {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
        //If you need the response, you can use it here
        int statuscode = [httpResponse statusCode];
        NSString *responseMsg = [NSHTTPURLResponse localizedStringForStatusCode:statuscode];
        NSLog(@" Status code: %d",statuscode );
        NSLog(@" Status msg: %@",responseMsg );

    }
    else 
    {
        // Handle error by looking at response and/or error values
        NSLog(@"%@",error);
    }
}

我的问题是 alertview 仅在 View 更改时显示片刻。它应该在我单击按钮时打开。可能是什么原因?如何解决?

编辑 1:

如果我向服务器发出异步请求,那么我将无法在我的表格 View 中设置这些数据。仅当发送同步请求时,我才能在我的 TableView 中设置这些数据,但它会阻止该应用程序。为什么会这样?

任何帮助将不胜感激。 谢谢你。

最佳答案

您正在主线程上发送 SynchronousRequest,因此它阻塞了您的 UI 线程。阅读多线程,您将获得有关此的各种教程。我可以建议您选择 GCD 或 NSOperation 和 NSOperationQueue。谷歌以上任何一项,您将获得相同的各种样本。

或者你可以像下面这样发送异步请求...

[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
        //Write code you want to call when data is received,
        //Like dismissing loading view and populating UI.
 }];

更新:

//Display alert view, before sending your request..
[alertview show];
//send first request
[NSURLConnection sendAsynchronousRequest:request1 queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){

            //Write code you want to call when data is received,
            //send second request
[NSURLConnection sendAsynchronousRequest:request2 queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){

            //Write code you want to call when data is received,
            //send third request
[NSURLConnection sendAsynchronousRequest:request3 queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
            //Write code you want to call when data is received,
            //dismiss alert view on main thread
dispatch_async(getmainqueue, ^(void) { 
// dismiss alert view... 
});
     }];
     }];

     }];

关于iphone - UIAlertView 迟到了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18531487/

相关文章:

javascript - 新的 react native 元素无法访问

ios - firebase auth 用户照片 URL 返回 404

php - 使用网络服务在 prestashop 下完整订单

iphone - 在 Xcode 中调试时如何打印整数值?

javascript - 如何创建可检测浏览器类型并进行相应重定向的 Google 协作平台脚本?

ios - 需要一些关于 iOS 项目面向对象设计的建议

c# - 如何在 JSON 中指定子类类型(在 REST Web 服务中)

web-services - 在 Delphi Win32 中使用 Thrift

iphone - 我可以将 UITableView 分组背景图案用于 UIView 吗?

iphone - 使用仪器进行内存分析