i am working in creating a custom class which is subclass of UIWebview. But its delegate functions are not being called. How should i set delegate for this class. I know this is a basic question but it will be really good if some one could answer it. Thanks in advance.
My Code:
.h file
@interface PollackWebView : UIWebView<UIWebViewDelegate> {
}
-(id)initWebview:(CGRect)frame;
@end
.m file:
@implementation PollackWebView
-(id)initWebview:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
}
return self;
}
-(void)SetMyDelegate {
self.delegate = self;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"shouldStartLoadWithRequest");
if ( navigationType == UIWebViewNavigationTyp开发者_如何学JAVAeLinkClicked ) {
// do something with [request URL]
return NO;
}
return TRUE;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(@"webViewDidStartLoad");
}
I think what your trying to do, you don't need a subclass. In the code you posted you have only used delegate methods, so instead of creating a subclass:
- Just use a normal UIWebView.
- In the class that creates and makes visible the webview, set the UIWebView's delegate to
self
just after you Alloc/init the UIWebView. - Then in the header of the class that creates/makes visible the webview add
<UIWebViewDelegate>
to the end of the@interface
line, just before the{
- Then put the delegate methods in the class's implementation.
精彩评论