这与我之前问过的问题有关,但原始问题已得到解答。希望可以开一个新的问题,如果不是,可以随意删除它。从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协议。