Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Tab bar programmatically

Tags:

ios

storyboard

enter image description here

I have created tabbar in storyboard .and i am trying to change default tab bar programmatically using following code.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure your segue name in storyboard is the same as this line
    if ([[segue identifier] isEqualToString:@"TAB"])
    {
        UIStoryboard* storyboard   = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        UITabBarController *tabBar = [storyboard instantiateViewControllerWithIdentifier:@"tabBar"];
        tabBar.selectedViewController = [tabBar.viewControllers objectAtIndex:1];
    }
}

But its not working.How i can do it? its not working at all.

Also i want to go back to home view controller when user select first tab.how we can achieve this functionality.

like image 598
user3883340 Avatar asked Jul 28 '14 07:07

user3883340


People also ask

What is tab bar controller in Swift?

A tab bar controller is a powerful UI component for iOS apps. It's a container view, and you use it to group view controllers together. They give your app's user access to the most important screens of your app.


1 Answers

From the current selected view controller you can change tab selected index using code below.

[self.tabBarController setSelectedIndex:1];

To add Tab bar controller to viewcontrollers hirarchy IF YOU ARE USING NAVIGATION AS BASE then you can use:

UIStoryboard* storyboard   = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        UITabBarController *tabBar = [storyboard instantiateViewControllerWithIdentifier:@"tabBar"];
        tabBar.selectedViewController = [tabBar.viewControllers objectAtIndex:1];

[self.navigationController pushViewController:tabBar animated:BOOL];
like image 143
Esha Avatar answered Sep 21 '22 14:09

Esha