raed raed - 5 months ago 24
Objective-C Question

How to call SuperClass delegate method from SubClass delegate method

I have a

SuperClass
that implements
<UIWebViewDelegate>
, in this class I implemented the method
webView:shouldStartLoadWithRequest:navigationType:


@interface SuperClass
...
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request1 navigationType:(UIWebViewNavigationType)navigationType {
// SuperClass treatment
}
...
@end


Then I have a
SubClass
that extend this
SuperClass
. the
SubClass
implements
<UIWebViewDelegate>
too, and the method
webView:shouldStartLoadWithRequest:navigationType:
as well :

@interface SubClass: SuperClass
...
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request1 navigationType:(UIWebViewNavigationType)navigationType {
// SubClass treatment
}
...
@end


The code works for me, because I need to do a specific treatment for the
SubClass
.
But in a specific case, I need to call the
SuperClass
webView:shouldStartLoadWithRequest:navigationType:
method.

I need the delegate to execute the
SuperClass
UIWebViewDelegate
methods.


I tried to use
super
to call the
SuperClass
method, but with no use!

Is that possible?

Answer

SuperClass

@interface ViewController () <UIWebViewDelegate>
@end

@implementation ViewController

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request1 navigationType:(UIWebViewNavigationType)navigationType 
{    
  NSLog(@"superview");
  return true;    
}

Subclass (ViewController1.m)

@interface ViewController1 () <UIWebViewDelegate>

@property (strong, nonatomic) IBOutlet UIWebView *webview;

@end

@implementation ViewController1

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request1 navigationType:(UIWebViewNavigationType)navigationType {

     NSLog(@"subclass");
     [super webView:webView shouldStartLoadWithRequest:request1 navigationType:navigationType];
     return true;

 }

- (IBAction)action:(id)sender {

    NSLog(@"loading");
    [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.co.in"]]];

 }

In the logs it is coming as:

2016-06-29 17:51:39.807 Test[31575:8224563] loading

2016-06-29 17:51:41.008 Test[31575:8224563] subclass

2016-06-29 17:51:41.008 Test[31575:8224563] superview

Comments