1

在我的项目中,我使用 tabBarController 作为 rootView 控制器,然后在我的一个选项卡上,添加我现有的 ToDoList 应用程序。我遇到的问题是:如果我在 AppDelegate 中使用此代码:ToDoList 加载为 RootView。但我希望它仅在选择适当的选项卡后显示。

- (void)applicationDidFinishLaunching:(UIApplication *)application {  
    //todoRootController.managedObjectContext = self.managedObjectContext;
    ToDoRootViewController *todoRootViewController = [[ToDoRootViewController alloc]initWithNibName:@"ToDoRootViewController" bundle:nil];
    NSManagedObjectContext *context = [self managedObjectContext];

    if (!context) {
        // Handle the error.
    }

    // Pass the managed object context to the view controller.
    todoRootViewController.managedObjectContext = context;
    UINavigationController *aNavigationController = [[UINavigationController alloc]                                                  
                                                     initWithRootViewController:todoRootViewController];
    self.navigationController = aNavigationController;

    [window addSubview:[navigationController view]];
    [window makeKeyAndVisible];
    [todoRootViewController release];

    [aNavigationController release];
}

我将 applicationDidFinishLaunching:(UIApplication *)application 方法替换为 CoreDataReceipeis 示例代码

- (void)applicationDidFinishLaunching:(UIApplication *)application {  
    todoRootController.managedObjectContext = self.managedObjectContext;

//////////////// same stuff
}

但随后它给出了“NSInternalInconsistencyException”,原因:“+entityForName:找不到实体名称“任务”的 NSManagedObjectModel

4

1 回答 1

0

利用 Interface Builder 设置在键入选项卡时将显示的选项卡视图控制器。

如果您必须以编程方式执行此操作,那么您应该查看 TabBarControllers 的文档。

- (void)applicationDidFinishLaunching:(UIApplication *)application { 

  tabBarController = [[UITabBarController alloc] init]; 

  MyViewController* vc1 = [[MyViewController alloc] init]; 

  MyOtherViewController* vc2 = [[MyOtherViewController alloc] init]; 

  NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil]; 

  tabBarController.viewControllers = controllers; 

  // Add the tab bar controller's current view as a subview of the window 
  [window addSubview:tabBarController.view];

} 
于 2011-06-03T17:53:13.150 回答