0

I have two tabbars upper and bottom, when I click on tabbaritem UIView show but when I click to another tabbar item the previous view still remain in controller. The code is

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{


    switch (item.tag) {
        case 0:
        {
            NSLog(@"home");
            homeViewController *homeVc= [[homeViewController alloc] initWithNibName:@"homeViewController" bundle:nil];
            [self.view insertSubview:homeVc.view belowSubview:tabBar];
            break;
        }
        case 1:
        {

             NSLog(@"compus");
    compusViewController *compusVc= [[compusViewController alloc] initWithNibName:@"compusViewController" bundle:nil];
        [self.view insertSubview:compusVc.view belowSubview:tabBar];
            break;

        } 
Kampai
  • 22,848
  • 21
  • 95
  • 95
Farid
  • 44
  • 1
  • 12
  • First you create a new instance of `UIViewController` every time tab bar is tapped, which will definitely lead to memory leak. You should cache and reuse them. Second, you should create a container view and add/remove your content in it. – yusuke024 Nov 17 '14 at 06:46
  • Also, are you sure that you have set `tag` property to all the tab bar items, bot top and bottom bars? You might consider using `[[theTabBar items] indexOfObject:item]` to get the indexes instead of `tag`. See http://stackoverflow.com/a/3766362/2168557 – yusuke024 Nov 17 '14 at 06:54
  • @SikhapolSaijit ok,if i create a container view for total views,so which method i call,to remove view... – Farid Nov 17 '14 at 07:00
  • There is no direct methods to simple remove all subviews. You have to call `removeFromSuperview` for all subviews. Luckily, the code is concise. Use `[self.containerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]`. – yusuke024 Nov 17 '14 at 07:20

1 Answers1

0

There is no direct methods to simple remove all subviews. You have to call removeFromSuperview for all subviews. Luckily, the code is concise. Use [self.containerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]. – Sikhapol Saijit

Farid
  • 44
  • 1
  • 12