這與我之前問過的問題有關,但原始問題已得到解答。希望可以開一個新的問題,如果不是,可以隨意刪除它。從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協議。