如何从SubClass委托方法调用SuperClass委托方法

rae*_*aed 3 delegates objective-c uiwebview ios

我有一个SuperClass实现<UIWebViewDelegate>,在这个类中我实现了方法webView:shouldStartLoadWithRequest:navigationType:

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

然后我有一个SubClass扩展此SuperClass。该SubClass工具<UIWebViewDelegate>也和方法webView:shouldStartLoadWithRequest:navigationType:,以及:

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

该代码对我有用,因为我需要对进行特殊处理SubClass。但是在特定情况下,我需要调用该SuperClass webView:shouldStartLoadWithRequest:navigationType:方法。

我需要委托来执行SuperClass UIWebViewDelegate方法。

我试图用来super调用该SuperClass方法,但是没有用!

那可能吗?

Res*_*der 5

超类

@interface ViewController () <UIWebViewDelegate>
@end

@implementation ViewController

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

子类(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"]]];

 }
Run Code Online (Sandbox Code Playgroud)

在日志中,它是作为:

2016-06-29 17:51:39.807测试[31575:8224563]正在加载

2016-06-29 17:51:41.008测试[31575:8224563]子类

2016-06-29 17:51:41.008测试[31575:8224563]超级视图

  • 它的工作原理是,在SuperClass上也需要&lt;UIWebViewDelegate&gt;。 (2认同)