Search code examples
objective-ciosios5uitabbar

On click on Tab bar item view is not changing in iphone


I want to use a Tab bar in one of the view for my application which is VIEW BASED APPLICATION. so i dragged a tab bar and added 2 more tab bar Items to make it totally 4.

I have Tagged each Tab Item with 1,2,3,4 with Myservices,History,RecentRequest and profile label respectively.

similarly i have added 4 Classes for each tab with name Myservices.h,.m,.xib name and same names as above for other tabs as well.

In this above Tabs 3 are subclass of UITableViewController and profile tab is subclass of UIVIewController. when i click on profile tab its showing tableView and i have done some modification in other views as well but on click on any TAB its showing same screen for the the all Tabs so can anyone tel me where i am going wrong.

This is my code I have added this code in class called Homepage.h and .m and tab bar is dragged in homepage.xib

//CODE homepage.h

IBOutlet UITabBar *myTabBar; 
UIViewController *myServicesViewController;
UIViewController *historyViewController;
UIViewController *recentRequestViewController;
UIViewController *profileViewController;
UIViewController *currentViewController;

@property (nonatomic, retain) IBOutlet UITabBar *myTabBar; 
@property (nonatomic, retain) UIViewController *myServicesViewController;
@property (nonatomic, retain) UIViewController *historyViewController;
@property (nonatomic, assign) UIViewController *currentViewController;
@property (nonatomic, assign) UIViewController *profileViewController;
@property (nonatomic, retain) UIViewController *recentRequestViewControll

//HomePage.m

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

    NSLog(@"didSelectItem: %d", item.tag);
  [self activateTab:item.tag];

}

-(void)activateTab:(int)index

{

 switch (index) {
 case 1:
        if (myServicesViewController == nil) {

            self.myServicesViewController=

            [[MyServices alloc] initWithNibName:nil bundle:nil];
        }

        [self.view insertSubview:myServicesViewController.view belowSubview:myTabBar];
        if (currentViewController != nil)
            [currentViewController.view removeFromSuperview];
        currentViewController = myServicesViewController;           
        break;
    case 2:
        if (historyViewController == nil) {
            self.historyViewController =
            [[History alloc] initWithNibName:nil bundle:nil];
        }
        [self.view insertSubview:historyViewController.view belowSubview:myTabBar];
        if (currentViewController != nil)
            [currentViewController.view removeFromSuperview];
        currentViewController = historyViewController;          
        break;
    case 3:
        if (recentRequestViewController == nil) {
            self.recentRequestViewController =
            [[RecentRequest alloc] initWithNibName:nil bundle:nil];
        }
        [self.view insertSubview:recentRequestViewController.view belowSubview:myTabBar];
        if (currentViewController != nil)
            [currentViewController.view removeFromSuperview];
        currentViewController = recentRequestViewController;            
        break;
    case 4:
        if (profileViewController == nil) {
            self.profileViewController =
            [[Profile alloc] initWithNibName:nil bundle:nil];
        }
        [self.view insertSubview:profileViewController.view belowSubview:myTabBar];
        if (currentViewController != nil)
            [currentViewController.view removeFromSuperview];
        currentViewController = profileViewController;          
        break;
    default:
        break;
   }
}


-(void)viewDidLoad

{

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed: @"bg.jpg"]];
self.view.backgroundColor = background;
[myTabBar setSelectedItem:[myTabBar.items objectAtIndex:0]];
[self activateTab:1];
[super viewDidLoad];

}

Click on any tab the view is not changing can anyone tell me where I am going wrong??


Solution

  • After going through code again and again i found out that i have missed one line of code that is [self activateTab:item.tag];

    in the

    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item method. 
    {
        [self activateTab:item.tag];
    }
    

    i have edited my code and it works fine.