Take the 2-minute tour ×

I have a universal view controller class which all of the view controller classes in my app inherit from which has the following loadView method:

- (void) loadView
    if (viewType == UIStandardViewControllerViewTypeUIView)
        UIView *view = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
        [self setView: view];
        [view release];
    else if (viewType == UIStandardViewControllerViewTypeUIScrollView)
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame]];
        [self setView: scrollView];
        [scrollView release];
    else if (viewType == UIStandardViewControllerViewTypeUITableViewPlain || viewType == UIStandardViewControllerViewTypeUITableViewGrouped)
        UITableViewStyle tableViewStyle; 

        if (viewType == UIStandardViewControllerViewTypeUITableViewPlain)
            tableViewStyle = UITableViewStylePlain;
            tableViewStyle = UITableViewStyleGrouped;

        UITableView *tableView = [[UITableView alloc] initWithFrame: [[UIScreen mainScreen] applicationFrame] style: tableViewStyle];
        [self setView: tableView];
        [tableView release];

    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
    [[self navigationItem] setBackBarButtonItem: backButton];
    [backButton release];

I have it done this way for a lot of reasons that I don't feel like getting into. Anyhow, as you'll notice, one of the view types to implement is a tableview. As we all know, a tableview needs a delegate and a datasource. I was wondering if it's possible to implement the <UITableViewDelegate, UITableViewDataSource> at run time when I know a tableView is the choice that was made?

If not, does anyone else have any ideas how I can do this without having to manually implement my delegate and datasource in the inheriting view controller class? If I implement the data source and delegate at compile time (normally) in my UIStandardViewController class, then I get warning because I need to implement the mandatory data source and delegate methods in my standard view controller class. Would you implement these and then just override them in child class? Or anyone have any idea of how I can do this cleanly?

UPDATE: Was wondering, if I just implemented the delegate and data source in my UIStandardViewController class, and also implemented empty versions of the required methods, would this be a lot of extra overhead when I didn't use a tableview?

share|improve this question
No it's not possible. But you might want to rethink your structure, why do you want to do this? –  basvk Jan 25 '12 at 15:33
a) In order to use loadView, you have to be able to initialize a view in it, b) in subclasses off of this, I attach stuff to the view like gesture recognizer and want to do it in loadview properly. I mean why does it matter, ultimately, when you do loadview you're going to implement one of these three as follows anyhow. –  CoDEFRo Jan 25 '12 at 15:41
Yeah, I see what you're asking here, but I think there's a more fundamental problem in your OO design here that should probably be addressed in a different way than what you're asking. –  DougW Jan 26 '12 at 3:53
@dougw any recommendations? –  CoDEFRo Jan 26 '12 at 4:47
@dougw the only thing I can think of is to split off a separate subclass of uitableviewcontroller which is what I'm trying to avoid so I don't have to replicate code –  CoDEFRo Jan 26 '12 at 5:14

1 Answer 1

up vote 3 down vote accepted

You can write a controller (just controller, not view controller) that implements data source and the table view delegate. You will only create an instance if needed.

Also note, that you are using the Factory pattern. You should use a class method to create new views. The signature would be something like +(UIView *)viewWithType:(ViewTypeStyle) viewTypeSyle)


#import <Foundation/Foundation.h>

@interface TableController : NSObject <UITableViewDataSource,UITableViewDelegate>



#import "TableController.h"

@implementation TableController

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    return 1;

    return 100;

    static NSString *cellIdentifier = @"MyCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    cell.textLabel.text = [NSString stringWithFormat:@"%i", indexPath.row];
    return cell;


#import "ViewController.h"
#import "TableController.h"

@interface ViewController ()
@property(nonatomic,retain) UITableView *tableView;
@property(nonatomic,retain) TableController *controller;

@implementation ViewController
@synthesize tableView = tableView_;
@synthesize controller = controller_;

    self.tableView = nil;
    self.controller= nil;
    [super dealloc];
- (void)didReceiveMemoryWarning
    [super didReceiveMemoryWarning];

#pragma mark - View lifecycle

- (void)viewDidLoad
    [super viewDidLoad];

    self.controller = [[[TableController alloc] init] autorelease];
    self.tableView = [[[UITableView alloc] initWithFrame:self.view.frame] autorelease];
    self.tableView.delegate = self.controller;
    self.tableView.dataSource= self.controller;

    [self.view addSubview:self.tableView];



share|improve this answer
Wasn't aware that what I was doing was an actual pattern. Why do you recommend a class method to create the views? –  CoDEFRo Jan 25 '12 at 16:03
If you add a class method to your view class, you don't need to create an object to create another object. Please note, that you'll find this pattern all over cocoa. Ie NSMutableArray *array = [NSMutableArray arrayWithCapacity:number] –  vikingosegundo Jan 25 '12 at 16:08
One more thing to notice: these method should auto release. –  vikingosegundo Jan 25 '12 at 16:09
Anyway you can give an example of controller that you're talking about? I want to accept this answer as correct just have no way of verifying the controller thing. Appreciate the help. –  CoDEFRo Feb 2 '12 at 20:40
But you are aware of MVC? –  vikingosegundo Feb 3 '12 at 1:13

Your Answer


