根據用戶從表格視圖中選擇的內容(表格視圖正從聯機加載的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無效並且地圖尚未加載到服務器中時會發生這種情況)。
'@try ... @ catch ... @ finally' blocks **僅用於捕獲拋出的異常**。如果你想檢查圖像是否加載(或你的應用程序中的任何正常事件),你必須找到另一種方式(_delegates_,_notifications_,如果...然後...其他...),因爲你在目前的情況下,這些街區的路線是錯誤的。 – holex 2012-08-08 08:25:37