如何从子类委托方法调用超类委托方法
How to call SuperClass delegate method from SubClass delegate method
我有一个 SuperClass
实现了 <UIWebViewDelegate>
,在这个 class 我实现了方法 webView:shouldStartLoadWithRequest:navigationType:
@interface SuperClass
...
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request1 navigationType:(UIWebViewNavigationType)navigationType {
// SuperClass treatment
}
...
@end
然后我有一个 SubClass
扩展这个 SuperClass
。 SubClass
也实现了 <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
方法,但是没有用!
这可能吗?
@interface SuperClass
- (void)handleWebViewShouldStartLoadWithRequest:(NSURLRequest *)request;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
[self handleWebViewShouldStartLoadWithRequest:request];
}
@end
在子类handleWebViewShouldStartLoadWithRequest:
中,可以调用[super handleWebViewShouldStartLoadWithRequest:request]
;
超级班
@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 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
我有一个 SuperClass
实现了 <UIWebViewDelegate>
,在这个 class 我实现了方法 webView:shouldStartLoadWithRequest:navigationType:
@interface SuperClass
...
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request1 navigationType:(UIWebViewNavigationType)navigationType {
// SuperClass treatment
}
...
@end
然后我有一个 SubClass
扩展这个 SuperClass
。 SubClass
也实现了 <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
方法,但是没有用!
这可能吗?
@interface SuperClass
- (void)handleWebViewShouldStartLoadWithRequest:(NSURLRequest *)request;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
[self handleWebViewShouldStartLoadWithRequest:request];
}
@end
在子类handleWebViewShouldStartLoadWithRequest:
中,可以调用[super handleWebViewShouldStartLoadWithRequest:request]
;
超级班
@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 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