如何在更改主视图控制器中的选项卡栏时加载不同的视图控制器?

时间:2012-02-22 07:03:33

标签: ios ipad uitabbarcontroller uisplitviewcontroller

我正在link创建一个iPad。在这个中,我需要在更改masterside中的选项卡时加载不同的viewcontrollers。我该如何实现呢?我创建了tabbar控制器,如下所示:在Appdelegate.m文件中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.

    tabBarController = [[UITabBarController alloc] init];

    StudentVC *stdntVC = [[[StudentVC alloc]initWithNibName:@"StudentVC" bundle:nil] autorelease];
    TeachersVC *teachersVC = [[[TeachersVC alloc]initWithNibName:@"TeachersVC" bundle:nil] autorelease];
    MasterViewController *masterViewController = [[[MasterViewController alloc] initWithNibName:@"MasterViewController" bundle:nil] autorelease];
    ConfigurationVC *configViewController = [[[ConfigurationVC alloc] initWithNibName:@"ConfigurationVC" bundle:nil] autorelease];

    UINavigationController *masterNavigationController = [[[UINavigationController alloc] initWithRootViewController:masterViewController] autorelease];
    UINavigationController *studentNavigationController = [[[UINavigationController alloc] initWithRootViewController:stdntVC] autorelease];
    UINavigationController *teacherNavigationController = [[[UINavigationController alloc] initWithRootViewController:teachersVC] autorelease];
    UINavigationController *configNavigationController = [[[UINavigationController alloc] initWithRootViewController:configViewController] autorelease];

    NSArray* controllers = [NSArray arrayWithObjects:studentNavigationController,teacherNavigationController,masterNavigationController, configNavigationController, nil];
    tabBarController.viewControllers = controllers;

    ShowDetailsVC *showViewController = [[[ShowDetailsVC alloc] initWithNibName:@"ShowDetailsVC" bundle:nil] autorelease];
    UINavigationController *detailNavigationController = [[[UINavigationController alloc] initWithRootViewController:showViewController] autorelease];


    self.splitViewController = [[[UISplitViewController alloc] init] autorelease];
    self.splitViewController.viewControllers = [NSArray arrayWithObjects:tabBarController, detailNavigationController, nil];

    self.splitViewController.delegate = showViewController;

    self.window.rootViewController = self.splitViewController;    

    stdntVC.detailsVC = showViewController;
    teachersVC.detailsVC = showViewController;
    masterViewController.detailsVC = showViewController;
    configViewController.detailsVC = showViewController;

    [self.window makeKeyAndVisible];

    return YES;
}

以下是截图:enter image description here 请分享您的想法。

1 个答案:

答案 0 :(得分:1)

您可以使用– tabBarController:didSelectViewController:的方法UITabBarControllerDelegate来了解选择了哪个viewController。并刷新您的主视图

https://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarControllerDelegate_Protocol/Reference/Reference.html