我正在使用UITableView。如果没有网络连接,那么会在viewDidload中引发异常。我viewDidLoad中的功能是:不能在异常情况下显示UIAlert
@try {
NSLog(@"Request URL = %@",URLString);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
URLWithString:URLString]];
NSData *response = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];
NSError *jsonParsingError = nil;
NSDictionary *tableData = [NSJSONSerialization JSONObjectWithData:response
options:0
error:&jsonParsingError];
// Grab whole data with data field in JSON
// responseArray = [tableData objectForKey:@"data"];
responseArray = [[NSMutableArray alloc]initWithArray:[tableData objectForKey:@"data"]];
for(int i = 0; i < responseArray.count; i++)
{
NSArray * tempArray = responseArray[i];
responseArray[i] = [tempArray mutableCopy];
}
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(280.0, 0.0, 40.0, 40.0)];
[btn setImage:[UIImage imageNamed:@"sort_icon.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(showActionSheet) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barbutton = [[UIBarButtonItem alloc]initWithCustomView:btn];
self.navigationItem.rightBarButtonItem = barbutton;
}
@catch (NSException *exception)
{
exceptionOccured = YES;
NSLog(@"Exception Ocurred");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error in connectivity" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
在的cellForRowAtIndexPath我这样做:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
@try {
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
tempDict = [responseArray objectAtIndex:indexPath.row];
return cell;
}
@catch (NSException *exception)
{
NSLog(@"Error in CEll Create");
NSLog(@"Draw Alert");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error in connectivity" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
和AlertViewDelegate功能我做
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[self.navigationController popViewControllerAnimated:YES];
}
现在的问题是,它没有显示警报每当出现异常并重新抛出异常并显示错误时
Thread 1: EXC_BAD_ACCESS(code=2, address=0x2)
任何帮助将不胜感激...
您应该重写代码以避免引发异常。它们不适用于网络连接不可用等正常情况。 –
仅仅因为你发现异常并不意味着它会自动消失。您只是有机会清理这种情况并将您的数据重置为“稳定”状态。在这里,您只是显示警报,并且不采取进一步的行动来纠正问题,因此异常情况仍然会下降并上升。 – Cyrille
@Cyrille那么,我应该怎么做才能阻止它创造更多的细胞? – Atul