Search code examples
objective-cuitableviewsuperclass

How can a superclass trigger an action in a subclass in objective-c?


So I have a tab-bar controller where each tab is a UITableView that extends from a superclass that I created. This superclass (obviously) extends UITableViewController.

Each UITableViewController represents a tabbed view and has its own model that pulls data that is specific to each TableView, so I need to separate these methods and variables from the superclass.

However, I have implemented "pull-to-refresh" in my superclass and would like to refresh the data that is held by the subclass from the update method that is called when pulling-to-refresh.

Do I basically have to cut my losses and implement pull-to-refresh individually within every UITableViewController in my tabbar, or is there a way of getting the update command to call a method inherent (but implemented differently) within each UITableViewController in my tabbar?


Solution

  • In your common superclass, create a method

    -(void)onRefresh {
    }
    

    that does nothing. Call this method when you need to trigger an action in a subclass.

    In each of your subclasses, implement a method with the same signature. These methods would be called when superclass calls

    [self onRefresh];