0

我正在根据用户从表格视图中选择的内容(表格视图是从在线加载的 plist 填充)从在线服务器(在本例中为 Google 站点)加载图像视图。该代码已经在工作,但我正在尝试为未加载的图像添加警报视图。这是我的表视图控制器。

#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];
    }
}

这将是我的地图视图控制器代码。

#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 无效且地图尚未加载到服务器时会发生这种情况)。

4

1 回答 1

0
`enter code here`@catch(NSException *ex)
{
NSString *str=[ex copy];
//write code for alert
}
于 2012-08-08T06:24:20.933 回答