1

I am creating an iPad as in the link. In this one I need to load different viewcontrollers when I change the tab in the masterside. How can I implement this? I have created the tabbar controller as follows: in the Appdelegate.m file

- (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;
}

Here is the screen shot: enter image description here Please share your ideas.

Community
  • 1
  • 1
Mithuzz
  • 1,091
  • 14
  • 43

1 Answers1

1

you may use method – tabBarController:didSelectViewController: of UITabBarControllerDelegate to know which viewController is selected. and you refresh your masterview

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

Alkimake
  • 1,797
  • 14
  • 30
  • but where should i add this code? In the appdelegate file? and how can I refresh masterview? – Mithuzz Feb 22 '12 at 08:49
  • you create your tabBar in appdelegate you can add this method to your app delegate and be sure you define your appdelegate as `UITabBarControllerDelegate` impelement this protocol to your .h file and add `tabBarController.delegate = self;` to your code. – Alkimake Feb 22 '12 at 08:53
  • to find out more information about protocols (delegates) take a look at http://iphonedevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html – Alkimake Feb 22 '12 at 08:53
  • now the delegate method is working, but how can I replace the view controllers in detail view? – Mithuzz Feb 22 '12 at 09:00
  • i got the following code: UIViewController* myReplacementVC = nil; if(viewController == VC1) myReplacementVC = myReplacementVC1; else myReplacementVC = myReplacementVC2; NSMutableArray* arr = [[NSMutableArray alloc] initWithArray:splitVC.viewControllers]; [arr replaceObjectAtIndex:1 withObject:myReplacementVC]; //index 1 corresponds to the detail VC splitVC.viewControllers = arr; [arr release];, but I don't understand how it will work. – Mithuzz Feb 22 '12 at 09:08
  • code simply replaces the viewController at index 1 of splitVC.viewControllers array depending on if statement with myReplacementVC1 or myReplacementVC2 – Alkimake Feb 22 '12 at 10:09
  • but what should be actually VC1? – Mithuzz Feb 22 '12 at 10:26
  • one of your navigationControllers (masterNavigationController, studentNavigationController,teacherNavigationController, childNavigationController) – Alkimake Feb 22 '12 at 10:42
  • i got it almost working but one more step to go, i used the following code - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { UIViewController* myReplacementVC = nil; if(viewController == studentNavigationController){ NSLog(@"1"); myReplacementVC = detailNavigationController; }else if(viewController == teacherNavigationController){ NSLog(@"12"); myReplacementVC = testNavigationController; } – Mithuzz Feb 22 '12 at 11:13
  • else if(viewController == masterNavigationController){ NSLog(@"13"); myReplacementVC = detailNavigationController; }else if(viewController == configNavigationController){ NSLog(@"14"); myReplacementVC = testNavigationController; } NSMutableArray* arr = [[NSMutableArray alloc] initWithArray:[self.splitViewController viewControllers]]; [arr replaceObjectAtIndex:1 withObject:myReplacementVC]; //index 1 corresponds to the detail VC self.splitViewController.viewControllers = arr; [arr release]; } – Mithuzz Feb 22 '12 at 11:14
  • but i am getting an error at self,splitViewController.viewControllers = arr; – Mithuzz Feb 22 '12 at 11:14