将 NSDictionary 从 AppDelegate 传递给 ViewController [英] Passing an NSDictionary to a ViewController from AppDelegate

查看:40
本文介绍了将 NSDictionary 从 AppDelegate 传递给 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我之前问的一个问题有关,但原始问题得到了回答.希望可以开一个新问题,如果没有,请随意删除.

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.

我正在尝试使用 tableviews 和 tabbar 构建一个简单的 iPhone 应用程序.除了标题和需要显示的数据类型之外,每个视图在编程上都是相同的.除此之外,它们的行为都相同.

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.

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

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:

AppDelegate.h

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

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 协议.

The RootViewController implements the UITableViewDataSource protocol.

推荐答案

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

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

在 RootViewController 类中创建一个 NSArray ivar 说tableDataArray",

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

创建一个新的 init 方法,该方法将包含要保留在此 ivar 中的数组:

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;

}

在tableView的数据源方法中使用tableDataArray.

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

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

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.

希望能帮到你

这篇关于将 NSDictionary 从 AppDelegate 传递给 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆