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...

Lizza
  • 2,769
  • 5
  • 39
  • 72
  • 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. – El Developer Feb 19 '12 at 00:30
  • 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... – Lizza Feb 19 '12 at 01:40
  • 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. – Sam May 07 '12 at 20:04
  • 1
    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. – Rob Reuss May 14 '12 at 00:34

2 Answers2

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.

Jonathan Cichon
  • 4,396
  • 16
  • 19
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

j_mcnally
  • 6,928
  • 2
  • 31
  • 46