Why can't I store a UIWebView's UIScrollView delegate property anymore?
Asked Answered
A

2

6

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

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:

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.

Any ideas?

I'm not using ARC...

Alarum answered 18/2, 2012 at 23:57 Comment(4)
Just a question, did you ever get to submit an app that was doing that? I thought that was prohibited by Apple. I want to do something similar though I don't know if it will get the app rejected.Stroke
Yeah it did get approved, but now it is time for an update, and I can't get it working so I am in trouble...Alarum
Why do you need to retain your delegate? This is evidence of a larger problem in the way you are constructing your view hierarchy. Delegates should always be declared weak, as retaining them doesn't make sense.Shopwindow
Based on the way you've described it, this isn't necessarily an issue of storing the delegate - it is not completely clear that the delegate value you're seeking to store is visible or set in the first place. Have you inspected webView.scrollview.delegate directly, itself to see if it is set in the first place? If it's already nil, then storing it to Olddelegate is going to give you nil.Syl
D
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.

Dola answered 9/5, 2012 at 15:17 Comment(0)
S
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

Sycophancy answered 19/2, 2012 at 0:2 Comment(2)
Is Olddelegate a property | @property (nonatomic, retain) NSObject* Olddelegate;Sycophancy
There is no Garbage Collection in iOS.Jeffreyjeffreys

© 2022 - 2024 — McMap. All rights reserved.