Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:18:34+00:00 2026-05-28T19:18:34+00:00

I have a universal view controller class which all of the view controller classes

  • 0

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;
        }
        else 
        {
            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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:18:34+00:00Added an answer on May 28, 2026 at 7:18 pm

    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)

    TableController.h

    #import <Foundation/Foundation.h>
    
    @interface TableController : NSObject <UITableViewDataSource,UITableViewDelegate>
    
    @end
    

    TableController.m

    #import "TableController.h"
    
    @implementation TableController
    
    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return 100;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        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;
    }
    @end
    

    ViewController.m

    #import "ViewController.h"
    #import "TableController.h"
    
    @interface ViewController ()
    @property(nonatomic,retain) UITableView *tableView;
    @property(nonatomic,retain) TableController *controller;
    @end
    
    @implementation ViewController
    @synthesize tableView = tableView_;
    @synthesize controller = controller_;
    
    
    -(void)dealloc
    {
        self.tableView = nil;
        self.controller= nil;
        [super dealloc];
    }
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Release any cached data, images, etc that aren't in use.
    }
    
    #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];
    
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    //...
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Universal app (iPhone/iPad) which has an iAd displayed at the bottom
I have a Universal app (iPhone/iPad) testing on IOS4.3. It has an iAd which
I have a universal app, where I am sharing the same controller for a
I am updating an iPhone app to an universal. I have different views controller
I have a simple class which does reverse geocoding, but it causes the app
I have the following code for my universal app but I'm getting this weird
in my universal app, I have a UIControl View inside of a UIScrollView. On
I recently upgraded my iPhone app to a universal binary. I have 2 view
My project is set up to be Universal. I have a view in which
I have a universal data type, which is passed by value, but does not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.