Multiple delegates per one object?

后端 未结 5 2030
无人共我
无人共我 2020-12-25 14:54

I have a UIScrollView that I need to subclass and within the subclass I need to attach the UIScrollViewDelegate so I can implement the viewFo

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-25 15:40

    Short answer: you don't. Delegates are typically a weak one-to-one relationship:

    @property (nonatomic, weak /*or assign*/) id delegate;
    

    Sometimes you will see a "listener" design pattern, which is the one-to-many form of delegates:

    - (void) addListener:(id)listener;
    - (void) removeListener:(id)listener;
    

    In your case, there doesn't appear to be a nice public override point in UIScrollView that allows subclasses to specify the viewForZoomingInScrollView. I would avoid making the UIScrollView its own delegate, if possible. You could make the UIViewController the UIScrollViewDelegate and have it provide the viewForZooming. Or you could make an intermediate view subclass which uses UIScrollView, provides the viewForZooming, and forwards the other delegate methods up.

提交回复
热议问题