Multiple delegates per one object?

后端 未结 5 2052
无人共我
无人共我 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条回答
  •  Happy的楠姐
    2020-12-25 15:19

    You don't want an object with 2 delegates. You want to keep your customScrollView keep the responsibility of its own UIScrollViewDelegate functions.

    To make your parentVC respond to the delegate methods of UIScrollView as well you will have to make a custom delegate inside your customScrollView.

    At the moment a UIScrollViewDelegate function gets called you will also call one of your delegate functions from your custom delegate. This way your parentVC will respond at the moment you want it to.

    It will look somewhat like this.

    CustomScrollView.h

    @protocol CustomDelegate 
    
    //custom delegate methods
    -(void)myCustomDelegateMethod;
    
    @end
    
    @interface CustomScrollView : UIScrollView 
    {
        id delegate
        //the rest of the stuff
    

    CustomScrollView.m

    -(void) viewForZoomingInScrollView
    {
        [self.delegate myCustomDelegateMethod];
        //rest of viewForZoomingInScrollView code
    

    ParentVC.h

    @interface CustomScrollView : UIViewController 
    {
        //stuff
    

    ParentVC.m

    -(void)makeCustomScrollView
    {
         CustomScrollView *csv = [[CustomScrollView alloc] init];
         csv.delegate = self;
         //other stuff
    
    }
    
    -(void)myCustomDelegateMethod
    {
       //respond to viewForZoomingInScrollView
    }
    

    I hope this fully covers your problem. Good luck.

提交回复
热议问题