2012-08-08 33 views
0

根据用户从表格视图中选择的内容(表格视图正从联机加载的plist填充),我从在线服务器(本例中为Google协作平台)加载图片视图。该代码已经工作,但我试图为未加载的图像添加警报视图。这是我的桌面视图控制器。如何为不加载图像的uiimageview添加try catch块?

#import "TableViewController.h" 
#import "MapViewController.h" 

@interface TableViewController() 

@end 

@implementation TableViewController 
@synthesize trailList; 
@synthesize trailView; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSURL *url = [NSURL URLWithString: @"https://sites.google.com/site/majorprojectgjk/Trails.plist"]; 
    trailList = [[NSMutableArray alloc] initWithContentsOfURL: url]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [trailList count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellid = @"trailCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellid]; 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellid]; 
    } 

    cell.textLabel.text = [trailList objectAtIndex: indexPath.row]; 
    return cell; 
} 

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString: @"pushView"]) 
    { 
     NSIndexPath *indexPath = [self.trailView indexPathForSelectedRow]; 
     MapViewController *mvc = segue.destinationViewController; 
     mvc.subjects = [trailList objectAtIndex: indexPath.row]; 
    } 
} 

这将是我的Map View控制器代码。

#import "MapViewController.h" 

@interface MapViewController() 

@end 

@implementation MapViewController 
@synthesize map; 
@synthesize subjects; 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear: (BOOL)animated]; 

    NSString *site = @"https://sites.google.com/site/majorprojectgjk/"; 
    NSLog(@"Display: %@", subjects); 
    NSString *mapString = @"Map"; 
    NSString *png = @".png"; 
    //NSString *url = [NSString stringWithFormat: @"%@%@%@%@",site, subjects, mapString, png];  

    @try 
    { 
     NSString *space = subjects; 
     if ([space rangeOfString:@" "].location == NSNotFound) 
     { 
      NSString *url = [NSString stringWithFormat: @"%@%@%@%@",site, subjects, mapString, png]; 
      map.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [[NSURL alloc] initWithString: url]]]; 
      NSLog(@"URL: %@", url); 
     } 

     else 
     { 
      NSLog(@"string contains space"); 
      NSString *removeSpace = [subjects stringByReplacingOccurrencesOfString:@" " withString:@""]; 
      NSString *url = [NSString stringWithFormat: @"%@%@%@%@",site, removeSpace, mapString, png]; 
      map.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [[NSURL alloc] initWithString: url]]]; 
      NSLog(@"URL %@", url); 
     } 
    } 

    @catch (NSException *ex) { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"The map could not load!" delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
      [alert show]; 
    } 

} 

正如我前面所说,这些代码工作得很好。该图像从URL完全正常下载,数据也从表视图中解析。我想知道如何使用@try @catch块来提醒用户UIImageView为空(当URL无效并且地图尚未加载到服务器中时会发生这种情况)。

+0

'@try ... @ catch ... @ finally' blocks **仅用于捕获抛出的异常**。如果你想检查图像是否加载(或你的应用程序中的任何正常事件),你必须找到另一种方式(_delegates_,_notifications_,如果...然后...其他...),因为你在目前的情况下,这些街区的路线是错误的。 – holex 2012-08-08 08:25:37

回答

0
`enter code here`@catch(NSException *ex) 
{ 
NSString *str=[ex copy]; 
//write code for alert 
} 
+0

请您详细说明一下吗? (NSException * ex) { NSString * str = [ex copy];}写入try block @catch之后的 – bumpfox 2012-08-08 06:53:08

+0

//编写警报的代码UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@“Error!” message:str delegate:self cancelButtonTitle:@“Ok”otherButtonTitles:nil]; [alert show]; } – user1423012 2012-08-08 06:57:15

+0

这不起作用:-( – bumpfox 2012-08-08 07:37:42