0
votes

I am adding a tabbarcontroller from a uiview controller. Please check my code:

UITabBarController *tabBarController = [[UITabBarController alloc] init];
            NSMutableArray *arrControllers = [[NSMutableArray alloc] init];
            for(int i = 0; i<arrTabs.count;i++){
                NSArray *arr = [arrTabs objectAtIndex:i];
                if([[arr objectAtIndex:0] isEqualToString:@"PICS"]){
                    picTabViewController *pics = [[picTabViewController alloc] initWithNibName:@"picTabViewController" bundle:nil];
                    UINavigationController *picsNVC = [[UINavigationController alloc] initWithRootViewController:pics];
                    picsNVC.tabBarItem.image = [UIImage imageNamed:@"tab-news.png"];
                    picsNVC.tabBarItem.title = [arr objectAtIndex:1];
                    [arrControllers addObject:picsNVC];
                }
                if([[arr objectAtIndex:0] isEqualToString:@"MAP"]){
                    mapTabViewController *maps = [[mapTabViewController alloc] initWithNibName:@"mapTabViewController" bundle:nil];
                    UINavigationController *mapsNVC = [[UINavigationController alloc] initWithRootViewController:maps];
                    mapsNVC.tabBarItem.image = [UIImage imageNamed:@"tab-news.png"];
                    mapsNVC.tabBarItem.title = [arr objectAtIndex:1];
                    [arrControllers addObject:mapsNVC];
                }
                if([[arr objectAtIndex:0] isEqualToString:@"HTML"]){
                    htmlTabViewController *html = [[htmlTabViewController alloc] initWithNibName:@"htmlTabViewController" bundle:nil];
                    UINavigationController *htmlNVC = [[UINavigationController alloc] initWithRootViewController:html];
                    htmlNVC.tabBarItem.image = [UIImage imageNamed:@"tab-news.png"];
                    htmlNVC.tabBarItem.title = [arr objectAtIndex:1];
                    [arrControllers addObject:htmlNVC];
                }
            }
            tabBarController.viewControllers = arrControllers;
            self.tabBarController.selectedIndex = 0;
            [self.view.window addSubview:tabBarController.view];

The tab bar controller is added as desired. But now I want to add a button to go back to previous page, or you can say remove tabbar and its viewcontrollers from the viewcontroller on which it was added. Can someone please suggest me how can I do it?
Please remember that I have added the tabbarcontroller from viewcontroller and not app delegate.

Regards
Pankaj

2
not getting you. can you elaborate this with image?Niru Mukund Shah
I need to add a button on my first page(uiviewcontroller) of tabcontroller which can remove complete tab controller.pankaj
@pankaj see my this answer stackoverflow.com/questions/13581838/…Paras Joshi

2 Answers

0
votes

I've taken one tabbar and alloc-init once, then I'll show & hide on different UIViews.
So there is no need to remove & alloc all the time.

Show Tabbar

[self showTabBar:self.tabBarController];

Hide Tabbar

[self hideTabBar:self.tabBarController];

Code for Showing -> It'll automatically appear by setting its 'Y'::

- (void) showTabBar:(UITabBarController *) tabbarcontroller
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.4];
    for(UIView *view in tabbarcontroller.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            if ([[UIScreen mainScreen] bounds].size.height == 568)
            {
                [view setFrame:CGRectMake(view.frame.origin.x, 519, view.frame.size.width, view.frame.size.height)];
            }
            else if ([[UIScreen mainScreen] bounds].size.height == 480)
            {
                [view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
            }
        }
        else
        {
            if ([[UIScreen mainScreen] bounds].size.height == 568)
            {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 519)];
            }
            else if ([[UIScreen mainScreen] bounds].size.height == 480)
            {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
            }
        }
    }
    [UIView commitAnimations];
}

Code for Hiding -> It'll automatically disappear by setting its 'Y'::

- (void) hideTabBar:(UITabBarController *) tabbarcontroller
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.4];
    for(UIView *view in tabbarcontroller.view.subviews)
    {
        if([view isKindOfClass:[UITabBar class]])
        {
            if ([[UIScreen mainScreen] bounds].size.height == 568)
            {
                [view setFrame:CGRectMake(view.frame.origin.x, 568, view.frame.size.width, view.frame.size.height)];
            }
            else if ([[UIScreen mainScreen] bounds].size.height == 480)
            {
                [view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
            }
        }
        else
        {
            if ([[UIScreen mainScreen] bounds].size.height == 568)
            {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 568)];
            }
            else if ([[UIScreen mainScreen] bounds].size.height == 480)
            {
                [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
            }
        }
    }

    [UIView commitAnimations];
}

Hopefully, it'll help you if don't want to alloc & release all the time during application.
Thanks.

0
votes

Just put this method in AppDelegate.m file and call this method when you want to remove tabbarcontroller from superview and set another view as a parentviewController

-(void)setMainView 
{

     yourViewController *masterViewController = [[[yourViewController alloc] initWithNibName:@"yourViewController" bundle:nil] autorelease];
     self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
     self.navigationController.navigationBar.hidden=YES;
     self.window.rootViewController = self.navigationController;

    CATransition *animation = [CATransition animation];
    [animation setDelegate:self];   
    [animation setType:kCATransitionFade];
    [animation setDuration:0.5];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:
                                  kCAMediaTimingFunctionEaseInEaseOut]];
    [[self.window layer] addAnimation:animation forKey:kAnimationKey];
}

and call above method with object of AppDelegate class For Ex..

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate setMainView];

i hope this helped you..