如何从标准UIViewController中获取UIWebView的UIScrollView委托方法? [英] How can I get a UIWebView's UIScrollView delegate methods called from within a standard UIViewController?

查看:139
本文介绍了如何从标准UIViewController中获取UIWebView的UIScrollView委托方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我只有一个标准的UIViewController,其中包含一个显示pdf的UIWebView。对于应用程序功能,我需要能够响应UIWebView的嵌套UIScrollView事件,如scrollViewWillBeginDragging,scrollViewDidScroll等。

So I just have a standard UIViewController with a UIWebView in it that displays a pdf. For the app functionality, I have need to be able to respond to the UIWebView's nested UIScrollView events like scrollViewWillBeginDragging, scrollViewDidScroll, etc.

我可以访问的唯一方法scrollView是(似乎是一个黑客)进入并通过子视图数组得到它:

The only way I can get access to the scrollView is to (it seems like a hack) go in and get it by the subviews array:

for (id subview in webView.subviews){
    if ([[subview class] isSubclassOfClass: [UIScrollView class]])  {
        UIScrollView * s = (UIScrollView*)subview;
        s.delegate = self;
        s.tag = 1;
        scrollView = s;
    }
}

但这似乎引入了更多的问题而不是它的价值,因为我丢失了原生的UIScrollView缩放功能。

But that seems to introduce more problems than it's worth, because I lose native UIScrollView stuff like zooming.

所以总结我需要的东西:

So to sum up what I'm needing:

将UIViewController类设置为UIWebView内UIScrollView的委托的最佳方法是什么?我是否需要对自己的UIWebView进行子类化,以便处理来自UIWebView内置的事件某种方式,然后以某种方式传递给UIViewController?那个过时的东西怎么办?

What is the best way to set my UIViewController class as the delegate of the UIScrollView inside of the UIWebView? Is there something I need to do with subclassing my own UIWebView so that it handles events from it's built in UIWebView a certain way and then pass it along somehow to the UIViewController? How does that passing thing work anyway?

请指教!

谢谢!

推荐答案


  1. 您是否检查过子视图中只有一个UIScrollView子类?在循环中的日志中查看。如果有多个,那么你只能使用你的代码拿起最后一个。

  1. Have you checked there is only one UIScrollView subclass in the subviews? Bung in a log in your loop to see. If there is more than one, then you'll only pick up the last one using your code.

如果只有一个UIScrollView子类,你可以尝试保存对其委托的引用,然后在您自己的委托方法中,在您完成业务后传递消息。

If there is just one UIScrollView subclass, you could try saving a reference to its delegate and then in your own delegate methods passing messages on after you have done your business.

所以,在你的循环中,类似

So, in your loop, something like

originalDelegate = s.delegate

然后对于委托方法,例如:

And then for the delegate methods, something like:

- (void) scrollViewDidScroll: (UIScrollView*) scrollView;
{
     // do your stuff

     [originalDelegate scrollViewDidScroll: scrollView];
}

您可能需要检查originalDelegate在调用之前是否响应选择器,即if([originalDelegate respondsToSelector:@selector(scrollViewDidScroll :))等。如果是我,我将首先实现UIScrollView委托协议中定义的所有12个委托方法。

You might need to check whether originalDelegate responds to the selector before calling it, i.e. if ([originalDelegate respondsToSelector: @selector(scrollViewDidScroll:)) etc. If it were me, I'd start by implementing all twelve delegate methods defined in the UIScrollView delegate protocol.

没有对此进行测试,因此有兴趣知道它是否可以正常工作。请注意,文档明确说明UIWebView不应该是子类

Not tested this, so will be interested to know if it can be made to work. Do note, the docs explicitly say that UIWebView "should not be subclassed"

这篇关于如何从标准UIViewController中获取UIWebView的UIScrollView委托方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆