tags:

views:

27

answers:

2

hi,

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 == UIWebViewNavigationTypeLinkClicked ) {

        // do something with [request URL]
        return NO;
    }

    return TRUE;

}

- (void)webViewDidStartLoad:(UIWebView *)webView {

    NSLog(@"webViewDidStartLoad");

}
A: 

Your class inherits the delegate instance variable from UIWebView. Setting the delegate works exactly the same way. Create a controller class that implements the UIWebViewDelegate protocol and the methods you need. Then instantiate the controller class and assign it using the delegate property.

muffix
can u suggest me some code to inherit the delegate......
pankaj
It automatically inherits if it is a subclass of UIWebView. The superclass is the one following the colon (:) in your .h file. If you create a new class, replace `NSObject` by `UIWebView`.
muffix
this is what I am doing but the delegate methods are not being called
pankaj
Show your code where you set the delegate.
muffix
please check my code
pankaj
can you please suggest me is it the correct way of setting delegate or there is something better
pankaj
+3  A: 

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.
Jonathan
i know that n have done that many times but now i wanted to do it this way... n its working fine.. thanks for commenting
pankaj
Setting `self.delegate=self` is a sign that something is really wrong. The whole idea of delegates is to delegate responsibility to *something else*. What you just did is breaking a core principle of Cocoa, and will only lead to troubles farther down the road.
PeyloW
@PeyloW, is that aimed at me or pankaj? I'm not saying `self.delegate=self` in fact that's what I'm saying not to do. I was saying the class thats creates the UIWebView to set the UIWebView's delegate to self (in other words set the UIwebView's delegate to the class that created it. `UIWebView *webview = [[UIWebView alloc] init];` `webview.delegate = self`
Jonathan
+1 on this. In most cases if you're subclassing foundation classes or things like UIWebView, you should think about your design. "Uses" is much more common, and usually the better design in Cocoa apps.
Firoze Lafeer
@Jonathan: My comment was a reply to the first comment by @pankaj. My bad for not correctly labeling it as such.
PeyloW
Oh no problem, just a little confusing.
Jonathan