【问题标题】:How to load a different view controller when changing the tab bar in the master view controller?更改主视图控制器中的标签栏时如何加载不同的视图控制器?
【发布时间】:2012-02-22 07:03:32
【问题描述】:

我正在创建一个 iPad,如 link。在这一个中,当我更改 masterside 中的选项卡时,我需要加载不同的视图控制器。我该如何实施?我按如下方式创建了 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;
}

这是屏幕截图: 请分享您的想法。

【问题讨论】:

  • 我已添加,请立即查看。

标签: ios ipad uitabbarcontroller uisplitviewcontroller


【解决方案1】:

您可以使用UITabBarControllerDelegate 的方法– tabBarController:didSelectViewController: 来知道选择了哪个viewController。然后你刷新你的主视图

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

【讨论】:

  • 但是我应该在哪里添加这段代码?在 appdelegate 文件中?以及如何刷新主视图?
  • 您在 appdelegate 中创建您的 tabBar 您可以将此方法添加到您的应用程序委托中,并确保您将您的 appdelegate 定义为 UITabBarControllerDelegate 将此协议添加到您的 .h 文件中并将 tabBarController.delegate = self; 添加到您的代码中.
  • 要了解有关协议(代表)的更多信息,请查看iphonedevelopertips.com/objective-c/…
  • 现在委托方法可以工作了,但是如何在细节视图中替换视图控制器?
  • 我得到以下代码: UIViewController* myReplacementVC = nil; if(viewController == VC1) myReplacementVC = myReplacementVC1;否则 myReplacementVC = myReplacementVC2; NSMutableArray* arr = [[NSMutableArray alloc] initWithArray:splitVC.viewControllers]; [arr replaceObjectAtIndex:1 withObject:myReplacementVC]; //索引1对应细节VC splitVC.viewControllers = arr; [arr release];,但我不明白它是如何工作的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
  • 2011-04-02
  • 2014-04-24
  • 1970-01-01
相关资源
最近更新 更多