将 NSDictionary 从 AppDelegate 传递到 ViewController
这与我之前提出的一个问题有关,但原来的问题已得到回答。希望可以提出一个新问题,如果没有,请随时删除它。
我正在尝试使用表格视图和选项卡栏构建一个简单的 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 协议。
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.
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.
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
#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
The RootViewController implements the UITableViewDataSource protocol.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的“RootViewController”是tableView的数据源(实现UITableViewDataSource协议)
在RootViewController类中创建一个NSArray ivar,说“tableDataArray”,
创建一个新的init方法,该方法将包含要在此ivar中保留的数组:
}
使用tableDataArray在tableView的数据源方法中。
然后,当您在循环中创建 RootViewController 时,您可以使用新的 init 方法设置 tableDataArray,并且可以使用不同的数组填充每个表视图。
希望这有帮助
If your "RootViewController" is the tableView's data source (implements the UITableViewDataSource protocol)
Create a NSArray ivar in the RootViewController Class say "tableDataArray",
Create a new init method that will include an array to be retained in this ivar:
}
Use the tableDataArray in the tableView's data source methods.
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.
hope this helps
答案取决于您的设计。
如果您所说的数据在每次运行中保持不变,您应该将该数据存放在视图控制器中,因为它是表视图基本配置的一部分。
如果数据随着时间的推移而变化,那么您应该将其放入应用程序委托中,然后使用具有指向该数据的指针的自定义视图控制器子类。查看 Xcode 中使用核心数据的模板项目之一。类之间的关系与您需要的相同,但您将使用数组而不是托管对象上下文。
The answer depends on your design.
If the data you're talking remains the same from run to run, you should park that data in the view controller because it is part of the basic configuration of the tableviews.
If the data evolves overtime, then you should put it in app delegate and then use a custom view controller subclass that has a pointer to that data. Look at one of the template projects in Xcode that use core data. The relationship between the classes is the same as what you need but you would use an array instead of a managed object context.