Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a problem with making <code>MySubclass</code> its own delegate. Presumably you don't want to run custom code for <em>all</em> of the <code>UIScrollViewDelegate</code> methods, but you have to forward the messages to the user-provided delegate whether you have your own implementation or not. So you could try to implement all of the delegate methods, with most of them just forwarding like this:</p> <pre><code>- (void)scrollViewDidZoom:(UIScrollView *)scrollView { [self.myOwnDelegate scrollViewDidZoom:scrollView]; } </code></pre> <p>The problem here is that sometimes new versions of iOS add new delegate methods. For example, iOS 5.0 added <code>scrollViewWillEndDragging:withVelocity:targetContentOffset:</code>. So your scrollview subclass won't be future-proof.</p> <p>The best way to handle this is to create a separate, private object that just acts as your scrollview's delegate, and handles forwarding. This dedicated-delegate object can forward <em>every</em> message it receives to the user-provided delegate, because it <em>only</em> receives delegate messages.</p> <p>Here's what you do. In your header file, you only need to declare the interface for your scrollview subclass. You don't need to expose any new methods or properties, so it just looks like this:</p> <h3>MyScrollView.h</h3> <pre><code>@interface MyScrollView : UIScrollView @end </code></pre> <p>All the real work is done in the <code>.m</code> file. First, we define the interface for the private delegate class. Its job is to call back into <code>MyScrollView</code> for some of the delegate methods, and to forward <em>all</em> messages to the user's delegate. So we only want to give it methods that are part of <code>UIScrollViewDelegate</code>. We don't want it to have extra methods for managing a reference to the user's delegate, so we'll just keep that reference as an instance variable:</p> <h3>MyScrollView.m</h3> <pre><code>@interface MyScrollViewPrivateDelegate : NSObject &lt;UIScrollViewDelegate&gt; { @public id&lt;UIScrollViewDelegate&gt; _userDelegate; } @end </code></pre> <p>Next we'll implement <code>MyScrollView</code>. It needs to create an instance of <code>MyScrollViewPrivateDelegate</code>, which it needs to own. Since a <code>UIScrollView</code> doesn't own its delegate, we need an extra, strong reference to this object.</p> <pre><code>@implementation MyScrollView { MyScrollViewPrivateDelegate *_myDelegate; } - (void)initDelegate { _myDelegate = [[MyScrollViewPrivateDelegate alloc] init]; [_myDelegate retain]; // remove if using ARC [super setDelegate:_myDelegate]; } - (id)initWithFrame:(CGRect)frame { if (!(self = [super initWithFrame:frame])) return nil; [self initDelegate]; return self; } - (id)initWithCoder:(NSCoder *)aDecoder { if (!(self = [super initWithCoder:aDecoder])) return nil; [self initDelegate]; return self; } - (void)dealloc { // Omit this if using ARC [_myDelegate release]; [super dealloc]; } </code></pre> <p>We need to override <code>setDelegate:</code> and <code>delegate:</code> to store and return a reference to the user's delegate:</p> <pre><code>- (void)setDelegate:(id&lt;UIScrollViewDelegate&gt;)delegate { _myDelegate-&gt;_userDelegate = delegate; // Scroll view delegate caches whether the delegate responds to some of the delegate // methods, so we need to force it to re-evaluate if the delegate responds to them super.delegate = nil; super.delegate = (id)_myDelegate; } - (id&lt;UIScrollViewDelegate&gt;)delegate { return _myDelegate-&gt;_userDelegate; } </code></pre> <p>We also need to define any extra methods that our private delegate might need to use:</p> <pre><code>- (void)myScrollViewDidEndDecelerating { // do whatever you want here } @end </code></pre> <p>Now we can finally define the implementation of <code>MyScrollViewPrivateDelegate</code>. We need to explicitly define each method that should contain our private custom code. The method needs to execute our custom code, and forward the message to the user's delegate, if the user's delegate responds to the message:</p> <pre><code>@implementation MyScrollViewPrivateDelegate - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { [(MyScrollView *)scrollView myScrollViewDidEndDecelerating]; if ([_userDelegate respondsToSelector:_cmd]) { [_userDelegate scrollViewDidEndDecelerating:scrollView]; } } </code></pre> <p>And we need to handle all of the other <code>UIScrollViewDelegate</code> methods that we don't have custom code for, and all of those messages that will be added in future versions of iOS. We have to implement two methods to make that happen:</p> <pre><code>- (BOOL)respondsToSelector:(SEL)selector { return [_userDelegate respondsToSelector:selector] || [super respondsToSelector:selector]; } - (void)forwardInvocation:(NSInvocation *)invocation { // This should only ever be called from `UIScrollView`, after it has verified // that `_userDelegate` responds to the selector by sending me // `respondsToSelector:`. So I don't need to check again here. [invocation invokeWithTarget:_userDelegate]; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload