Search code examples
iosobjective-cuitabbaruitabbaritem

Open UIViewController upon clicking on UITabbarItem


I have created a UIViewController with a UITabbar in it.

enter image description here

I did not use UITabbarController because I wanted UITabbar on the top of the screen.

Upon clicking tab1, I want to present controller1 and on clicking tab2 I want to present controller 2. I don't want the tabbar to hide. I want to display the controller beneath the tabbar.

@interface MTLeaderFactoViewController () <UITabBarDelegate>
@property (weak, nonatomic) IBOutlet UITabBar *tabBar;
@end

@implementation MTLeaderFactoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    if (item.tag == 0) {
        NSLog(@"item tag 0");
    } else {
        NSLog(@"item tag 1");
    }
}
@end

My questions:

1) didSelectItem method is not triggered even after using UITabbarDelegate

2) What is the most elegant way of displaying the controller when clicked on a button? I don't want to use segue as all the controllers are in different storyboards. For now, I plan to do

Controller1 *fp = [Controller1 controllerStoryboard:STORYBOARD_COURSE];
[self addChildViewController:fp];
[self.view addSubview:fp.view];
[fp didMoveToParentViewController:self];

EDIT 1:

    Controller1 *fp = [Controller1 controllerStoryboard:STORYBOARD_COURSE];
    [self addChildViewController:fp];
    [self.view addSubview:fp.view];
    [fp didMoveToParentViewController:self];

Tried this but it hides the tab bar. I want to utilize the space beneath the tab bar to display the controller


Solution

  • What you need to do is have a basecontroller class which will contain a tabbar(programatically created) then you can achive the desired output heres a sample baseController that i created,

    import UIKit
    
    class BaseViewController: UIViewController,UITabBarDelegate{
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let myTabBar = UITabBar()
            myTabBar.frame = CGRect(x: 0, y: 60, width:self.view.frame.size.width, height: 50)
    
            let one = UITabBarItem()
            one.title = "one"
            one.tag = 1
            let two = UITabBarItem()
            two.title = "two"
            two.tag = 2
            myTabBar.setItems([one,two], animated: false)
            self.view.addSubview(myTabBar)
            myTabBar.delegate = self
    
        }
    
    
        func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
            switch item.tag  {
            case 1:
                let controller = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
                addChildViewController(controller!)
                view.addSubview((controller?.view)!)
                controller?.didMove(toParentViewController: self)
                break
            case 2:
                let controller = storyboard?.instantiateViewController(withIdentifier: "ViewController")
                addChildViewController(controller!)
                view.addSubview((controller?.view)!)
                controller?.didMove(toParentViewController: self)
                break
            default:
                break
            }
    
        }
    
    }
    

    View Controller class :

    import UIKit
    
    class ViewController: BaseViewController {
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
                    // Do any additional setup after loading the view, typically from a nib.
        }
    
    
    }
    

    SecondView Controller :

    import UIKit
    
    class SecondViewController: BaseViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Do any additional setup after loading the view.
        }
    
    
    
    }