UIWebView不调用UIScrollViewDelegate

UIWebView不调用UIScrollViewDelegate

本文介绍了UIWebView不调用UIScrollViewDelegate方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的iPhone应用程序中,我将uiwebview的委托设置为文件所有者,但未调用uiscrollviewdelegate方法

In my iphone app i set delegate of my uiwebview to files owner but it is not calling uiscrollviewdelegate methods

我的代码是

- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"webViewDidStartLoad  ");
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
     NSLog(@"webViewDidFinishLoad  ");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

     NSLog(@"scrollViewDidEndDecelerating  ");
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    NSLog(@"scrollViewDidScroll  ");
}

但是我得到

webViewDidStartLoad
webViewDidFinishLoad

这些消息在控制台中.

我怎么了

预先感谢

推荐答案

UIWebView delegate 属性仅允许您为 UIWebViewDelegate 协议.(请查看此处以获取 UIWebView 参考.)

UIWebView's delegate property only allows you to set the delegate for the UIWebViewDelegate protocol. (Look here for UIWebView reference.)

UIWebView 本身就是 UIScrollViewDelegate ,因为它从其中包含的 UIScrollView 接收委托调用.不幸的是,您没有对该 UIScrollView 的API访问权限,因此无法更改其委托.因此,它仍然被硬编码到 UIWebView 本身.

UIWebView is in itself a UIScrollViewDelegate, in that it receives the delegate calls from the UIScrollView it contains. Unfortunately, you have no API-access to that UIScrollView, so you cannot change its delegate; thus, it remains hard-coded to the UIWebView itself.

现在,有几种方法可以尝试获取那些委托消息.

Now, there are several ways you can try and get those delegate messages.

获取 UIScrollViewDelegate 调用的一种方法是将 UIWebView 子类化(如果可行).无论如何,您都应该非常小心,因为根据您的操作,您可能会破坏 UIWebView 的完整性,并且某些功能可能会停止工作(可能在将来的某些SDK版本中).此外,Apple不鼓励这样做,尽管它不会使您的应用程序被拒绝(据许多报告称这样做的人,并且在App Store的批准中没有问题).

One way for you to get UIScrollViewDelegate calls is subclassing UIWebView, if that works. In any case, you should be very careful, because depending on what you do you might break the UIWebView integrity and some features could stop working (possibly with some future SDK release). Furthermore, Apple discourages doing that, although it will not make your app rejected (as per many people who have reported doing that and having no problems with the App Store approval).

另一种方法可能是遍历 UIWebView subviews ,直到找到 UIScrollView 对象,然后更改其委托.

Another approach could be iterating through the subviews of your UIWebView until you find a UIScrollView object and then change its delegate.

UIScrollView* scrollView = nil;
for (UIView* subview in [webView subviews]) {
   if ([subview isKindOfClass:[UIScrollView class]]) {
      scrollView = (UIScrollView*)subview;
      scrollView.delegate = <YOUR_DELEGATE_HERE>;
      break;
   }
}

我不确定这是否可靠或可能破坏任何东西,因为 UIWebView 与它的 UIScrollView 子级之间的耦合非常紧密,在任何地方都没有描述,但是您可以试试.总而言之,我认为这并不比对 UIWebView 进行子类化要好得多.

I am not sure if this is reliable or may break anything, since the coupling between UIWebView and its UIScrollView child is very tight and not described anywhere, but you can have a try. All in all, I don't think this is much better than subclassing UIWebView.

最后,第三种方法是定义自己的 UIWindow 类型.这将允许您覆盖一些关键方法( sendEvent hitTest:event:),以便您可以在 被分派之前触摸它们在视图级别.这将为您提供处理它们的机会,并在需要时阻止它们达到其原始目标.

Finally, a third approach is defining your own UIWindow type. This will allow you to override a few key methods (sendEvent or hitTest:event:) so that you can intercept the touches before they are dispatched at the view level. This will give you a chance to handle them and, if required, prevent them from reaching their original target.

这是最干净的解决方案,尽管它需要做更多的工作,并且将需要您定义一个自定义的 UIWindow ,您可以在其中进行所有的触摸识别/调度.

This is the cleanest solution, although it is more work and it will require you to define a custom UIWindow where you do all the touch recognizing/dispatching.

此处.

这篇关于UIWebView不调用UIScrollViewDelegate方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 07:35