DipakSonara
DipakSonara

Reputation: 2596

Changing UIViewController in UITabBarController on clicking the tab

I have UITabBarController with 4 tabs. I have individual 4 views on each of the tab of the UITabBarController. I want to change a UIViewController of second tab of the UITabBarController whenever i click the third tab of the UITabBarController.

Upvotes: 2

Views: 3095

Answers (1)

saadnib
saadnib

Reputation: 11145

You can do this by using UITabBarController's delegate as -

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if ([tabBarController selectedIndex] == 2)
    {
        NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:[tabBarController viewControllers]];

        NewViewController *nvc = [[NewViewController alloc] init];

        [arr replaceObjectAtIndex:1 withObject:nvc];

        [nvc release];

        [tabBarController setViewControllers:arr];
    }
}

Upvotes: 5

Related Questions