1

我有一个相当复杂的问题,如果有人能回答我将非常感激。所以基本上我有一个 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 方法加载。

4

0 回答 0