Search code examples
ipadtabbar

iPad add tabBar with in detail view


I want to show 3 different screens depending on the option user selects

screen where i have 3 options

They are 1. Type Prescription 2.Scribble Prescription 3. Prescription Form

How can i switch views for those 3?

Thanks


Solution

  • UITabBarControllers like SplitViewControllers were intended to only be the Root View Controller and hence you cannot nest a TabBarController in another view, but you can however nest a UITabBar in a view.

    I added the Tabbar to the details view at the bottom, a Navigation bar at the top and then a placeholder view between them. All in Interface Builder!, You will want to switch everything on with the autosize on the Placeholder view.

    Next, Implement the UITabBarDelegate, you will need:

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

    from that you can use item.tag which if you give each item a unique tag in Interface Builder will let you know which tab the user clicked. I setup defined values for mine:

    #define VIEW_TAB_A 0
    #define VIEW_TAB_B 1
    #define VIEW_TAB_C 2
    

    Then you will then want to...

    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    [self switchToView:item];
    

    }

    - (void) switchToView : (UITabBarItem*) item {
    
        if( currentViewController != nil ) {
            [currentViewController viewWillDisappear:NO];
            [currentViewController.view removeFromSuperview];               
        }
    
    switch(item.tag) {
        case VIEW_TAB_A:
            currentViewController = self.viewA;
            break;
        case SCAN_VIEW_TAB_B:
            currentViewController = self.viewB;
            break;
        case PROMOTIONS_VIEW_TAB_C:
            currentViewController = self.viewC;
            break;
    }
    
    UIView *aView = currentViewController.view; 
    
    aView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    aView.frame = placeholderView.frame;
    
    [currentViewController viewWillAppear:NO];
    
    [self.view insertSubview:aView aboveSubview:placeholderView];
    if( currentViewController != nil ) {
        [currentViewController viewDidDisappear:NO];
    }
    [currentViewController viewDidAppear:NO];
    

    } These are my notes that i learned from a similar question, search harder next time