2009-10-30 83 views
0

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

回答

1

如果你的「RootViewController的」是的tableView的數據源(實現UITableViewDataSource協議)

創建在RootViewController的類說「tableDataArray」一個NSArray的伊娃,

創建一個新的init方法,其中將包括一個被保持在該實例變量數組:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil tableDataSource:(NSArray*)tableData { 
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) { 
    // Custom initialization 
    tableDataArray = [tableData retain]; 

} 
return self; 

}

使用tableDataArray在的tableView的數據源方法。

然後,當您在循環中創建RootViewController時,您可以使用新的init方法設置tableDataArray,並且可以讓每個表視圖填充不同的數組。

希望這可以幫助

0

答案取決於您的設計。

如果您正在講話的數據從運行到運行保持不變,您應該將該數據停放在視圖控制器中,因爲它是tableview基本配置的一部分。

如果數據發生超時,則應將其放入應用程序委託中,然後使用具有指向該數據的指針的自定義視圖控制器子類。查看Xcode中使用核心數據的模板項目之一。類之間的關係與你需要的一樣,但是你會使用一個數組而不是一個託管對象上下文。

相關問題