Rani
Rani

Reputation: 3453

How to get the navigation bar when the tabbar controller is clicked?

I have an application in which I am trying to dynamically switch the tabbar tab through the code. The tab switches properly. When I click on any tab the didSelectController method of the tab is called and my problem is when I click on any tab the tab on which I am performing switching of views, its navigation bar disappears and its tabbar image and title also disappears.

This is my code. In the appdelegate:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    NSString *clockswitch = [[NSUserDefaults standardUserDefaults]objectForKey:@"clock"];
    UIViewController *desiredController = nil;
    if ([clockswitch isEqualToString:@"digital"]) {
        desiredController = [[DigitalClockViewController alloc] initWithNibName:@"DigitalClockViewController" bundle:nil ];


    }
    else {
        desiredController = [[AnalogClockViewController alloc]initWithNibName:@"AnalogClockViewController" bundle:nil];


    }
    NSMutableArray *controllersCopy = [self.tabBarController.viewControllers mutableCopy];
     [controllersCopy replaceObjectAtIndex:0 withObject:desiredController];
    self.tabBarController.viewControllers = controllersCopy;

}

Upvotes: 0

Views: 128

Answers (3)

akk
akk

Reputation: 135

   if ([clockswitch isEqualToString:@"digital"]) 
{
    desiredController = [[DigitalClockViewController alloc] initWithNibName:@"DigitalClockViewController" bundle:nil ];
    navigationController=[[UINavigationController alloc] initWithRootViewController:desiredController];
    [self presentModelViewController:navigationController animated:YES];
    [navigationController release];
    [desired... release];


}
else {
    desiredController = [[AnalogClockViewController alloc]initWithNibName:@"AnalogClockViewController" bundle:nil];

...//same code
}

Upvotes: 0

Paul Lynch
Paul Lynch

Reputation: 19789

You code is changing the order in which your view controllers appear in the tab bar, not changing the selected view controller as you seem to wish. This may result in the same view controller appearing twice in the bar, which may have unintended side effects.

Use selectedIndex or selectedViewController to change the selection. You want to select one of the controller that is already there, not alloc/init a new one; and, as Johannes has said, the entry in the list of controllers with be a UINavigationController with a rootViewController that is an instance of your class (isKindOf:).

Upvotes: 1

Johannes Fahrenkrug
Johannes Fahrenkrug

Reputation: 44846

If you want each view controller within your tab bar controller to have a navigation bar, you have to "wrap" each view controller in a navigation controller and then but the navigation controller into the viewControllers property of your tab bar controller. You wrap it like so:

UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:desiredController];

Upvotes: 0

Related Questions