2012-02-22 93 views
1

我正在創建一個iPad,如link。在這個我需要加載不同的視圖控制器,當我改變主界面的選項卡。我怎樣才能實現這個?我創建了的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 請分享您的想法。

+0

你能發表截圖嗎? – user523234 2012-02-22 08:31:31

+0

我已添加,請現在檢查。 – Mithuzz 2012-02-22 08:42:32

回答

1

您可以使用UITabBarControllerDelegate的方法– tabBarController:didSelectViewController:來知道選擇哪個viewController。並刷新你的主視圖

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

+0

但我應該在哪裏添加此代碼?在appdelegate文件中?以及如何刷新masterview? – Mithuzz 2012-02-22 08:49:05

+0

你可以在appdelegate中創建你的tabBar,你可以將這個方法添加到你的應用程序委託中,並且確保你的appdelegate定義爲'UITabBarControllerDelegate',將這個協議推送到你的.h文件並且添加'tabBarController.delegate = self;'到你的代碼中。 – Alkimake 2012-02-22 08:53:16

+0

想要了解更多關於協議的信息(代表),請看http://iphonedevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html – Alkimake 2012-02-22 08:53:50

相關問題