I can't get UIWebViewDelegate to work
I'm trying to implement a UIWebViewDelegate in my application and I cannot for the life of me get it to work. I would really appreciate a second set of eyes on this.
I know the name MapViewController is confusing, but the WebView is controlling a map (not UIMapView).
Here's the bulk of the code:
MapViewController.h
#import <UIKit/UIKit.h>
#import <UIKit/UIWebView.h>
@interface MapViewController : UIViewController<UIWebViewDelegate> {
IBOutlet UIWebView *webView;
}
@property (nonatomic, retain) IBOutlet UIWebView *webView;
@end
MapViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
webView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"map" ofType:@"html"]isDirectory:NO]];
[webView loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(@"Done loading.");
}
- (BOOL)w开发者_StackOverflowebView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"hi");
return NO;
}
- (void)dealloc {
webView.delegate = nil;
[webView release]
[super dealloc];
}
Thanks for any help!
Does the initial page you're loading actually display? Are you sure you connected your webview in Interface Builder?
go interface builder , connect the delegate to the file's owner , then it should work.
- Open Interface Builder
- Right click on the Web View object
- Drag the delegate onto files owner.
Things to check:
- if map.html is in the bundle. Put it on the root of the bundle, not on resources.
- if it is, right click on it, and choose get info. Verify on TARGETS if the target is checked for the file.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"hi");
return YES; // for all supported oreintation.
}
精彩评论