Ipad 如何为未加载图像的uiimageview添加try-catch块?
我正在根据用户从表视图中选择的内容从在线服务器(本例中为Google站点)加载图像视图(表视图是通过在线加载的plist填充的)。这方面的代码已经在运行,但我正在尝试为未加载的图像添加一个警报视图。这是我的表视图控制器Ipad 如何为未加载图像的uiimageview添加try-catch块?,ipad,uiimageview,try-catch,xcode4.4,ios,Ipad,Uiimageview,Try Catch,Xcode4.4,Ios,我正在根据用户从表视图中选择的内容从在线服务器(本例中为Google站点)加载图像视图(表视图是通过在线加载的plist填充的)。这方面的代码已经在运行,但我正在尝试为未加载的图像添加一个警报视图。这是我的表视图控制器 #import "TableViewController.h" #import "MapViewController.h" @interface TableViewController () @end @implementation TableViewController
#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无效且映射尚未加载到服务器时会发生这种情况)。在写入try块@catch(NSException*ex){NSString*str=[ex copy];//编写警报UIAlertView*警报的代码=[[UIAlertView alloc]initWithTitle:@“错误!”消息:str委托:自取消按钮:@“确定”其他按钮:nil];[alert show];}您可以仅使用NSString*str=ex尝试吗;请粘贴您编写的代码。
@try…@catch…@finally
块仅用于捕获抛出的异常。如果要检查映像是否加载(或应用程序中的任何正常事件),必须找到另一种方法(委托、通知、if…then…else
etc…),因为在当前情况下,这些块的路径错误。
`enter code here`@catch(NSException *ex)
{
NSString *str=[ex copy];
//write code for alert
}