2009-10-30 82 views
0

这与我之前问过的问题有关,但原始问题已得到解答。希望可以开一个新的问题,如果不是,可以随意删除它。从AppDelegate将NSDictionary传递给ViewController

我想用tableviews和tabbar构建一个简单的iPhone应用程序。除了标题和需要显示的数据种类之外,每个视图都以编程方式相同。除了它们都表现相同之外。

当前在我的AppDelegate中的代码处理视图控制器到不同选项卡的分配并相应地设置标题。然而,我无法弄清楚的是,如何将特定的对象数组(每个选项卡的每个数组都不相同)传递给每个分布式视图控制器,以便tableview使用它。

希望你能帮助我。我的代码如下所示:

AppDelegate.h

#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; 

@end 

AppDelegate.m

#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]; 
} 
@end 

的RootViewController的实现UITableViewDataSource协议。

回答

1

如果你的“RootViewController的”是的tableView的数据源(实现UITableViewDataSource协议)

创建在RootViewController的类说“tableDataArray”一个NSArray的伊娃,

创建一个新的init方法,其中将包括一个被保持在该实例变量数组:

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

} 
return self; 

}

使用tableDataArray在的tableView的数据源方法。

然后,当您在循环中创建RootViewController时,您可以使用新的init方法设置tableDataArray,并且可以让每个表视图填充不同的数组。

希望这可以帮助

0

答案取决于您的设计。

如果您正在讲话的数据从运行到运行保持不变,您应该将该数据停放在视图控制器中,因为它是tableview基本配置的一部分。

如果数据发生超时,则应将其放入应用程序委托中,然后使用具有指向该数据的指针的自定义视图控制器子类。查看Xcode中使用核心数据的模板项目之一。类之间的关系与你需要的一样,但是你会使用一个数组而不是一个托管对象上下文。

相关问题