Take the 2-minute tour ×

This is related to a question I asked earlier, but the original question was answered. Hope it's okay to open a new question, if not, feel free to delete it.

I'm trying to build a simple iPhone application using tableviews and a tabbar. Every view is the same programmatically, except for the title and the kind of data which needs to be displayed. Except for that they all behave the same.

Currently code in my AppDelegate handles the distribution of viewcontrollers to the different tabs and sets the title accordingly. What I can't figure out however, is how to pass a specific array of objects (every array differs per tab) to each distributed viewcontroller so that it's used by the tableview.

Hope you can help me out. My code follows below:


#import <UIKit/UIKit.h>

@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
  UIWindow *window;
  UITabBarController *tabBarController;

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;



#import "RootViewController.h"
#import "MyAppDelegate.h"

@implementation MyAppDelegate.h

@synthesize window, tabBarController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"1", @"Id", 
                    @"My Name", @"Name", 
                    @"My Type", @"Type",
                    @"My Meta", @"Meta",
                    @"My Excerpt Text", @"Excerpt",
                    @"My Body Text", @"Body",
                    @"icon.jpg", @"Icon", 
                    @"image.jpg", @"Image", nil];

  NSArray *array = [[NSArray alloc] initWithObjects:row1, nil];
  self.events = array;

  NSArray *tableControllersConfig = [NSArray arrayWithObjects:
                                 [NSDictionary dictionaryWithObjectsAndKeys:@"test1", @"DataSource", @"Title of the Tableview", @"Title", @"icon.png", @"Icon", nil],

  NSMutableArray *tableControllers = [NSMutableArray array];
  for (NSDictionary *configDict in tableControllersConfig) {
    RootViewController *controller = [[RootViewController alloc] initWithNibName:@"TableView" bundle:nil];
    controller.tableView.delegate = controller;
    controller.title = [configDict valueForKey:@"Title"];
    controller.tabBarItem.image = [UIImage imageNamed: [configDict valueForKey:@"Icon"]];
    [tableControllers addObject:controller];
    [controller release];

  self.tabBarController.viewControllers = [NSArray arrayWithArray:tableControllers];
  [window addSubview:tabBarController.view];
  [row1 release];
  [array release];

- (void)dealloc {
    [tabBarController release];
    [window release];
    [super dealloc];

The RootViewController implements the UITableViewDataSource protocol.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

If your "RootViewController" is the tableView's data source (implements the UITableViewDataSource protocol)

Create a NSArray ivar in the RootViewController Class say "tableDataArray",

Create a new init method that will include an array to be retained in this ivar:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil tableDataSource:(NSArray*)tableData {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    // Custom initialization
    tableDataArray = [tableData retain];

return self;


Use the tableDataArray in the tableView's data source methods.

Then when you create RootViewController in your loop you can set the tableDataArray with the new init method, and you can have each the table views populated with different array.

hope this helps

share|improve this answer

The answer depends on your design.

If the data you're talking remains the same from run to run, you should park that data in the view controller because it is part of the basic configuration of the tableviews.

If the data evolves overtime, then you should put it in app delegate and then use a custom view controller subclass that has a pointer to that data. Look at one of the template projects in Xcode that use core data. The relationship between the classes is the same as what you need but you would use an array instead of a managed object context.

share|improve this answer

Your Answer


