为什么我不能再存储UIWebView的UIScrollView委托属性?

时间:2022-07-01 00:57:44

Before ios5 I was able to access a UIWebView's UIScrollView delegate like this:

在ios5之前,我可以访问UIWebView的UIScrollView委托如下:

for (id subview in webView1.subviews){
    if ([[subview class] isSubclassOfClass: [UIScrollView class]])  {
        UIScrollView * s = (UIScrollView*)subview;
        OldDelegate = s.delegate;//OldDelegate is type id
                    s.delegate = self;
    }
}

Now, I know that's not the right way to do it, but at the time (as far as I understood) it was the only way to do it. iOS 5 has changed that, so I am trying to do it the iOS 5 way:

我知道这不是正确的方法,但在当时(据我所知),这是唯一的方法。ios5改变了这一点,所以我尝试用ios5的方式来做:

UIScrollView * s = webView.scrollView;
Olddelegate = s.delegate;
s.delegate = self;

But either way I try to do it, the value of my OldDelegate object is 0x0. It is really important that I retain this delegate value, but try as I might, I'm just getting 0x0.

但是不管我怎么做,我的OldDelegate对象的值都是0x0。保留这个委托值非常重要,但是尽可能地尝试,我只是得到了0x0。

Any ideas?

什么好主意吗?

I'm not using ARC...

我不使用电……

2 个解决方案

#1


1  

The scrollView property of UIWebView is a subclass of UIScrollView. This private class, called _UIWebViewScrollView does not use the delegate property, it is always nil. Maybe you could do some refactoring and get the real scrollview-delegate, but i'm almost 100% sure apple will reject your app doing so.

UIWebView的scrollView属性是UIScrollView的子类。这个名为_UIWebViewScrollView的私有类不使用委托属性,它总是nil。也许你可以做一些重构,得到真正的scrollview-delegate,但我几乎100%肯定苹果会拒绝你的应用。

#2


0  

Yes, if your using arc, its possible that its doing garbage collection on your unretained Olddelegate, you can either mark the file to not use arc with -fno-objc-arc and handle the retain and release on your own like you used to. Or you can implement strong references as described here to help you retain the variable. https://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

是的,如果您使用arc,它可能会在未保留的Olddelegate上进行垃圾收集,您可以标记文件,使其不使用带有-fno-objc-arc的arc,并像以前一样自己处理retain和release。或者可以实现这里描述的强引用,以帮助您保留变量。https://developer.apple.com/library/ios/ releasenotes / ObjectiveC RN-TransitioningToARC /介绍/ Introduction.html

#1


1  

The scrollView property of UIWebView is a subclass of UIScrollView. This private class, called _UIWebViewScrollView does not use the delegate property, it is always nil. Maybe you could do some refactoring and get the real scrollview-delegate, but i'm almost 100% sure apple will reject your app doing so.

UIWebView的scrollView属性是UIScrollView的子类。这个名为_UIWebViewScrollView的私有类不使用委托属性,它总是nil。也许你可以做一些重构,得到真正的scrollview-delegate,但我几乎100%肯定苹果会拒绝你的应用。

#2


0  

Yes, if your using arc, its possible that its doing garbage collection on your unretained Olddelegate, you can either mark the file to not use arc with -fno-objc-arc and handle the retain and release on your own like you used to. Or you can implement strong references as described here to help you retain the variable. https://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html

是的,如果您使用arc,它可能会在未保留的Olddelegate上进行垃圾收集,您可以标记文件,使其不使用带有-fno-objc-arc的arc,并像以前一样自己处理retain和release。或者可以实现这里描述的强引用,以帮助您保留变量。https://developer.apple.com/library/ios/ releasenotes / ObjectiveC RN-TransitioningToARC /介绍/ Introduction.html