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

时间:2016-06-29 11:42:38

标签: ios objective-c uiwebview delegates

我有SuperClass实施<UIWebViewDelegate>,在本课程中我实施了方法webView:shouldStartLoadWithRequest:navigationType:

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

然后我有SubClass扩展此SuperClassSubClass也实现<UIWebViewDelegate>,方法webView:shouldStartLoadWithRequest:navigationType:也是如此:

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

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

我需要代理人执行SuperClass UIWebViewDelegate方法。

我尝试使用super来调用SuperClass方法,但没有用!

这可能吗?

2 个答案:

答案 0 :(得分:5)

<强>父类

@interface ViewController () <UIWebViewDelegate>
@end

@implementation ViewController

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

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

 }

在日志中它将作为:

  

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

     

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

     

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

答案 1 :(得分:0)

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

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

@end

在子类的handleWebViewShouldStartLoadWithRequest:中,您可以调用[super handleWebViewShouldStartLoadWithRequest:request];