2011-04-22 48 views
1

我在做存儲一些數據的核心數據基於iPhone應用程序。傳遞managedObjectContext到的UITabBarController發出

它有一個UITabBarController作爲根視圖控制器(RootViewController)。標籤欄控制器由下式給出的應用程序委託兩個視點控制器 - 第一個是的UIViewController一個實例,它表示應用程序,第二個是,其用於顯示所述數據的UITableViewController的標題畫面。

這是使用核心數據我的第一個iPhone應用程序。我讀過,建立這種應用程序的正確方法是創建和應用程序委託初始化managedObjectModelmanagedObjectContextpersistentStoreCoordinator對象,然後按引用傳遞的managedObjectContext到子視圖控制器。這是我做到的。

但是,我沒能在managedObjectContext對象傳遞到標籤欄控制器我在初始化我的應用程序委託的applicationDidFinishLaunching:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions { 
    RootViewController *rootViewController = [[RootViewController alloc] init]; 
    rootViewController.managedObjectContext = self.managedObjectContext; 
    [window addSubview:rootViewController.view]; 
    [window makeKeyAndVisible]; 
    [rootViewController release]; 
    return YES; 
} 

即使正確顯示標籤欄和加載標題畫面視圖控制器,它的managedObjectContext仍然爲零。我無法弄清楚我做錯了什麼。我還嘗試通過向其添加新的保留屬性來傳遞字符串RootViewController

RootViewController.h讀,因爲它遵循:

@interface RootViewController : UITabBarController { 
@private 
NSManagedObjectContext *managedObjectContext; 
} 

@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext; 

@end 

我RootViewController的的viewDidLoad方法:

- (void)viewDidLoad { 
[super viewDidLoad]; 

NSLog(@"%@", self.managedObjectContext); 

ObiadViewController *obiadVC = [[ObiadViewController alloc] init]; 
ObiadListNavController *obiadListVC = [[ObiadListNavController alloc] init]; 

obiadVC.managedObjectContext = self.managedObjectContext; 
obiadListVC.managedObjectContext = self.managedObjectContext; 

NSArray *vcs = [NSArray arrayWithObjects:obiadVC, obiadListVC, nil]; 

self.viewControllers = vcs; 

[obiadVC release]; 
[obiadListVC release]; 

}

我還檢查了managedObjectContext是不是在應用程序委託零,只是然後傳遞給RootViewController實例。這就像所有的RootViewController的ivars得到重置。它只發生在這一點上。稍後,當我從表視圖控制器傳遞一個字符串到詳細視圖控制器時,一切都很好。

我希望我明確自己。我現在感覺很無能。

+0

什麼的NSLog(@ 「%@」,self.managedObjectContext);輸出在你的TabBarController.m?如果它是零,你實際上是把你的managedObjectContext爲零。不應該'obiadVC.managedObjectContext = self.managedObjectContext;'是'obiadVC.managedObjectContext = self.delegate.managedObjectContext;'? – 2011-04-22 22:11:50

+0

不是'RootViewController * rootViewController = [[RootViewController alloc] initWithNibName:@「RootViewController」bundle:nil]'而不是簡單的alloc/init?; – 2011-04-22 22:47:10

回答

2

UITabBarController Class Reference明確指出的UITabBarController是不應該被繼承:

此類不適合子類。

在這種情況下,你可以實例化你的UITabBarController和應用程序委託的applicationDidFinishLaunching添加視圖控制器到它:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    tabBarController = [[UITabBarController alloc] init]; 

    FirstViewController *firstViewController = [[FirstViewController alloc] init]; 
    SecondViewController *secondViewController = [[SecondViewController alloc] init]; 

    firstViewController.managedObjectContext = self.managedObjectContext; 
    secondViewController.managedObjectContext = self.managedObjectContext; 

    NSArray *vcs = [NSArray arrayWithObjects:firstViewController, secondViewController, nil]; 
    tabBarController.viewControllers = vcs; 

    [window addSubview:tabBarController.view]; 
    [window makeKeyAndVisible]; 

    [firstViewController release]; 
    [secondViewController release]; 

    return YES; 
} 

希望它能幫助。

+3

你會怎麼做故事板?因爲您無法以編程方式訪問視圖控制器,或者更確切地說,不需要。 – MrJD 2012-04-27 05:08:36

相關問題