我创建了一个UIWebView,并将一个本地HTML文件传递给它,它完全打开。该页面包含一个带有几个热点的非常大的图像。当点击其中一个热点时,我想在我的代码中运行自定义方法。我已经覆盖了我在代码中的内容(如下所示),但是 - (BOOL)webView:ShouldStartLoadWithRequest ....不会触发。我应该在我的控制台中“点击”,但没有任何反应。这是.h和.m文件:
//
// MapController.h
#import <UIKit/UIKit.h>
@interface MapViewController : UIViewController <UIWebViewDelegate>
{
IBOutlet UIWebView *webView;
NSString *currentProgram;
}
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@property (nonatomic, retain) NSString *currentProgram;
-(void)initWithProgram:(NSString *)program;
-(void)stageClicked;
@end
//
// MapController.m
#import "MapViewController.h"
@implementation MapViewController
@synthesize currentProgram, webView;
-(void) initWithProgram:(NSString *)program
{
self.currentProgram = program;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
//GenerateURL
NSMutableString *mapPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] mutableCopy];
[mapPath appendString:[@"/" mutableCopy]];
[mapPath appendString:[currentProgram mutableCopy]];
[mapPath appendString:[@"/Map" mutableCopy]];
NSMutableString *htmlFilePath = [mapPath mutableCopy];
[htmlFilePath appendString:[@"/index.html" mutableCopy]];
NSLog(@"Map Path: %@",mapPath);
NSMutableString *HTMLData = [NSString stringWithContentsOfFile:htmlFilePath encoding:NSUTF8StringEncoding error:nil];
[mapPath replaceOccurrencesOfString:@"/" withString:@"//"options:0 range:NSMakeRange(0, [mapPath length])];
[mapPath replaceOccurrencesOfString:@" " withString:@"%20"options:0 range:NSMakeRange(0, [mapPath length])];
[self.webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",mapPath]]];
//[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void) viewWillAppear:(BOOL)animated
{
self.title = @"Map";
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
//WebView Override
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"Click");
NSURL *url = [request URL];
if (![[url scheme] hasPrefix:@"Local"]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else{
[self stageClicked];
}
return YES;
}
- (void) stageClicked {
UIAlertView *nwAlert = [[UIAlertView alloc] initWithTitle:@"Info"
message:@"Link Clicked!"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[nwAlert show];
[nwAlert release];
}
@end
有人可以解释为什么会这样,或者更确切地说,为什么不这样做。当然,如何解决这个问题。
由于