简体   繁体   中英

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?

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

@interface SuperClass
- (void)handleWebViewShouldStartLoadWithRequest:(NSURLRequest *)request;

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
     [self handleWebViewShouldStartLoadWithRequest:request];
}

@end

In the handleWebViewShouldStartLoadWithRequest: of subclass, you can call [super handleWebViewShouldStartLoadWithRequest:request] ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM