这与我之前提出的一个问题有关,但原始问题已得到解答。希望可以打开一个新问题,如果没有,请随时删除它。
我正在尝试使用 tableviews 和 tabbar 构建一个简单的 iPhone 应用程序。每个视图在编程上都是相同的,除了标题和需要显示的数据类型。除此之外,它们的行为都相同。
目前我的 AppDelegate 中的代码处理视图控制器到不同选项卡的分配并相应地设置标题。然而,我无法弄清楚的是如何将特定的对象数组(每个数组每个选项卡不同)传递给每个分布式视图控制器,以便表格视图使用它。
希望你能帮助我。我的代码如下:
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 协议。