How to subclass UIScrollView and make the delegate property private

前端 未结 5 1041
悲&欢浪女
悲&欢浪女 2020-12-04 09:03

Here is what I want to achieve:

I want to subclass an UIScrollView to have additional functionality. This subclass should be able to react on scrolling, so i have to

5条回答
  •  爱一瞬间的悲伤
    2020-12-04 09:40

    Another option is to subclass and use the power of abstract functions. For instance, you create

    @interface EnhancedTableViewController : UITableViewController 
    

    There you override some delegate method and define your abstract function

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        // code before interception
        [self bfTableView:(UITableView *)tableView didSelectRowAtIndexPath:indexPath];
        // code after interception
    }
    
    - (void)bfTableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {};
    

    Now all you need to do is to subclass your EnhancedTableViewController and use your abstract functions instead of delegate ones. Like this:

    @interface MyTableViewController : EnhancedTableViewController ...
    
    - (void)bfTableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        //overriden implementation with pre/post actions
    };
    

    Let me know if there is anything wrong here.

提交回复
热议问题