iOS ViewDidLoad / WillLoad等NSURLConnection

时间:2012-08-22 10:56:07

标签: iphone objective-c ios uitableview nsurlconnection

我有一个相当复杂的问题,如果有人能回答我会非常感激。所以基本上我有一个tableView,如果你选择一行,它将转到另一个视图。此detailView将访问Web服务器并检索数据。我的问题是找不到服务器。

由于互联网连接不良或服务器是内部服务器等而无法访问服务器时,会显示警告消息。但是当我在警报消息上单击“确定”然后在表格视图中我选择另一行时,警报消息不会出现。它甚至不进入第二类(在我的情况下是jsonviewcontroller)。

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Navigation logic may go here. Create and push another view controller.
NSLog(@"DID SELECT");
Item *selectedItem = (Item *)[self.fetchedObjectsArray objectAtIndex:indexPath.row];
NSString * urlString = [CONST_FEED_DISCRIPTION_URL stringByAppendingString:selectedItem.guid];
NSDate * dateString = selectedItem.date;
JsonViewController *jsonViewController = [[JsonViewController alloc] initWithURLString:urlString date:dateString];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" 
                                                               style:UIBarButtonItemStyleBordered
                                                              target:nil
                                                              action:nil];
self.navigationItem.backBarButtonItem = backButton;
self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:CONST_NAVIGATIONBAR_COLOR];
[self.navigationController pushViewController:jsonViewController animated:YES];
[backButton release];
[jsonViewController release];
}

当你点击另一行时,它会进入这个方法,并改变导航栏的颜色等,如代码中所写,但并没有完全进入jsonviewcontroller。虽然第一次围绕它。我想它可能是因为jSonviewcontroller的视图从未加载,因为我正在从viewwillappear访问该服务,并且因为视图永远不会出现它不会进入viewdidload。我不知道我在这里失踪了什么。

- (void)viewWillAppear:(BOOL)animated
 {
     NSMutableURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://x"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
_rssFeedDetailViewConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    while(!finished) {
      [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  }

}

NSURLConnection的委托方法也已实现 -

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
responseData = [[NSMutableData alloc] init];
  }

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
      [responseData appendData:data];
      _json_string = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

  }

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    if ([error code] == kCFURLErrorNotConnectedToInternet) {
        // if we can identify the error, we can present a more precise message to the user.
        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedString(@"No Connection Error", @"Error message displayed when not connected to the Internet.") forKey:NSLocalizedDescriptionKey];
         NSError *noConnectionError = [NSError errorWithDomain:NSCocoaErrorDomain code:kCFURLErrorNotConnectedToInternet userInfo:userInfo];
         [self handleError:noConnectionError];
} else {
     // otherwise handle the error generically
     [self handleError:error];
  }
     finished = FALSE;
     [responseData release];
     [connection release];
   // Show error message
 }

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
      finished = TRUE;
      [responseData release];
      [connection release];
 }

如果有人可以帮我解决这个问题,真的很棒。谢谢。我的问题是 - 为什么我的JSONVIEWCONTROLLER不是第二次从didselectrow方法加载。

0 个答案:

没有答案