简体   繁体   English

类别和UIWebViewDelegate

[英]Categories and UIWebViewDelegate

I see the following code: 我看到以下代码:

@interface FViewController (UIWebViewDelegate) <UIWebViewDelegate>
@end

@implementation FViewController

@end


@implementation FViewController (UIWebViewDelegate)

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

}

.....

@end

Basically, FViewController is used to show a web view. 基本上,FViewController用于显示Web视图。 What's the purpose of making it a category as the above? 使其成为上述类别的目的是什么?

Also what does this do: 另外这是做什么的:

@interface FViewController (UIWebViewDelegate) <UIWebViewDelegate>

This doesn't make much sense to me. 这对我来说没有多大意义。 This code makes FViewController both implement the UIWebViewDelegate protocol, and create a category on it. 此代码使FViewController既实现UIWebViewDelegate协议,又在其上创建类别。 The only possible reason for doing this is a scenario in which you extend UIWebView, so it needs additional delegate methods. 这样做的唯一可能原因是扩展UIWebView的方案,因此它需要其他委托方法。 If this is not what you want to do, just make your class implement the delegate, as per usual: 如果这不是您想要执行的操作,请照常使类实现委托:

@interface FViewController <UIWebViewDelegate> 

Check out also this question and answer: Defining categories for protocols in Objective-C? 还可以查看以下问题和答案: 在Objective-C中定义协议的类别吗?

The author of this code is creating a category on FViewController that makes it conform to UIWebViewDelegate . 这段代码的作者正在FViewController上创建一个类别,使其符合UIWebViewDelegate Presumably, this category only implements UIWebViewDelegate methods. 据推测,该类别仅实现UIWebViewDelegate方法。 That can be useful if you want the source for that delegate be separate from the rest of the source. 如果您希望该委托的来源与其他来源分开,那将很有用。 Especially in large classes, adding categories to the class instead of putting it al in the class definition can make everything more readable. 特别是在大型类中,将类别添加到类中而不是将其放到类定义中可以使所有内容更具可读性。 The categories could be in separate source files, which leaves the core functionality of the class in the main implementation file for that class. 类别可以位于单独的源文件中,从而将类的核心功能保留在该类的主要实现文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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