0

我有一个用户将用来登录我的应用程序的登录视图。我使用 NSURLConnection 异步请求块连接到我的 Web 服务并来回传递数据。

我有一个奇怪的问题,如果用户第一次登录,它工作正常。但是如果他们注销,然后尝试再次登录,请求被发送但从不响应(我为所有场景设置了警报视图;即超时、数据 = nil、错误!= nil 等)。它只是与我显示的加载屏幕一起坐在那里,从不做任何事情。

我只是错过了什么吗?

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         if ([data length] > 0 && error == nil){
             if( data != nil ) {
                  //Handle Data Here
                  .............    
             } else {
                 [loadingView removeFromSuperview];
                 loadingView = nil;
                 BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Network Error" message:@"Network connection was successful, but there was a problem with data transfer.\nPlease try again."];
                 [alert setCancelButtonWithTitle:@"OK" block:nil];
                 [alert show];             }
         }
         else if ([data length] == 0 && error == nil){
             [loadingView removeFromSuperview];
             loadingView = nil;
             BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Network Error" message:@"Network connection was successful, but there was a problem with data transfer.\nPlease try again."];
             [alert setCancelButtonWithTitle:@"OK" block:nil];
             [alert show];
         }
         else if (error != nil && error.code == NSURLErrorTimedOut){
             [loadingView removeFromSuperview];
             loadingView = nil;
             BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Network Error" message:@"The network request timed out.\nPlease check your internet connection and try again."];
             [alert setCancelButtonWithTitle:@"OK" block:nil];
             [alert show];
         }
         else if (error != nil){
             [loadingView removeFromSuperview];
             loadingView = nil;
             BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Network Error" message:@"There was a problem connecting to the network.\nPlease check your internet connection and try again."];
             [alert setCancelButtonWithTitle:@"OK" block:nil];
             [alert show];
         }
     }];
4

1 回答 1

0

好的解决了这个问题。是我用来通知我的视图我完成处理/保存响应数据并执行转场的委托分配的问题。因为在 viewDidLoad 中设置了委托,所以当下一个视图出现并接管委托时,登录视图失去了委托控制。只需将委托分配移动到 viewWillAppear(或 viewDidAppear)。

于 2013-04-21T03:14:31.940 回答