简体   繁体   English

实现委托方法,但仍然收到警告?

[英]Implementing delegate methods, but still getting a warning?

I'm getting a peculiar warning, specifically this one: 我收到一个特殊的警告,特别是以下警告:

warning: class does not implement the 'UIWebViewDelegate' protocol

... when I have these methods in my application: ...当我在应用程序中使用以下方法时:

-(void)webViewDidStartLoad {
    ...
}

-(void)webViewDidFinishLoad {
    ...
}

... and even after setting the UIWebView's delegate to self , I still get this warning, and the methods are never called ( NSLog provides no results.) ...即使将UIWebView的委托设置为self ,我仍然会收到此警告,并且永远不会调用方法( NSLog提供结果)。

Can anyone pinpoint what is happening here? 任何人都可以查明这里发生了什么吗? Thanks in advance! 提前致谢!

I don't think your delegate methods are correct. 我认为您的委托方法不正确。 Shouldn't they be: 他们不应该是:

- (void)webViewDidStartLoad:(UIWebView *)webView
{
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
}

As '@No on in particular' says, your methods aren't declared correctly. 正如“ @No on”特别指出的那样,您的方法未正确声明。 That is why the methods aren't called at all. 这就是为什么根本不调用方法的原因。

The warning has a different reason. 该警告具有不同的原因。 It is because your interface definition doesn't declare that it implements the UIWebViewDelegate protocol. 这是因为您的接口定义没有声明它实现了UIWebViewDelegate协议。

@interface MyClass : NSObject <UIWebViewDelegate> {
...
}
...
@end

Whether or not the interface declares the protocol or not is actually of no consequence at runtime, it is just a hint to the compiler that allows it to issue warnings like the one you are seeing, or warnings if you don't implement the methods. 无论接口是否声明协议实际上在运行时都没有关系,这只是对编译器的提示,允许它发出警告(如您看到的警告)或警告(如果您未实现这些方法)。

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

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