0

I am using this code

 let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewController(withIdentifier: "PrintMainViewController")
            self.navigationController!.pushViewController(controller, animated: true)

and also added:

self.tabBarController?.tabBar.isHidden = false
Dhara
  • 35
  • 8
  • explain in detail so that we can help you. – jarvis12 Apr 04 '19 at 13:23
  • want to show tababar when user clicks from sidemenu but it is not showing.but when it is from homecontroller it will showing. – Dhara Apr 04 '19 at 13:26
  • Are you sure that `PrintMainViewController` is a subclass of a tab bar controller? The line `self.tabBarController?.tabBar.isHidden = false` will most likely do absolutely nothing in your code as `self.tabBarController` is `nil` if this is the same view controller as with the first snippet. If anything try `(controller as! UITabBarController).tabBar.isHidden = false`. If it crashes then this controller is not a subclass of tab bar controller. – Matic Oblak Apr 04 '19 at 13:27
  • @Dhara You need to change the selection of **tabBarController** while clicking from a side menu instance of **pushViewController** if you want to show tab bar in all. – Nikunj Kumbhani Apr 04 '19 at 13:29
  • No PrintMainViewController is not a subclass. it comes from sidemenu – Dhara Apr 04 '19 at 13:29
  • can u pls explain in brief?? @Nikunj Kumbhani – Dhara Apr 04 '19 at 13:30
  • @Dhara What is the side menu are you use any third-party Library? – Nikunj Kumbhani Apr 04 '19 at 13:30
  • yes swrevealviewcontroller i m using. – Dhara Apr 04 '19 at 13:31
  • @Dhara **Great, I have already answered this question please check this** https://stackoverflow.com/a/51725803/10150796 – Nikunj Kumbhani Apr 04 '19 at 13:32
  • thanks @NikunjKumbhani – Dhara Apr 04 '19 at 13:36

2 Answers2

3

If I understand your hierarchy correctly you have a tab bar controller which has navigation controllers in it. So basically any of the tabs can push additional view controllers and tab bar is still visible.

Now you want to push some new controller on the currently selected view controller in the tab bar and you want to do it from another part of the app, another view controller that has no relation to tab bar.

The quickest way to do that is to expose a static instance of your tab bar view controller. This will only work if you always have only 1 tab bar controller in your application (probably 99% of the applications).

First add a current instance to your tab bar view controller:

class MyTabBarViewController: UITabBarController {

    static private(set) var currentInstance: MyTabBarViewController?

    override func viewDidLoad() {
        super.viewDidLoad()
        MyTabBarViewController.currentInstance = self
    }

}

So when view loads a static value is assigned and can now be accessed anywhere in your project via MyTabBarViewController.currentInstance.

The rest is then just accessing the currently selected view controller and pushing a new view controller. Something like this should do:

let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "PrintMainViewController")
(MyTabBarViewController.currentInstance?.selectedViewController as? UINavigationController)?.pushViewController(controller, animated: true)
Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
  • Thank u @Matic Oblak – Dhara Apr 04 '19 at 14:37
  • Its not working for me... Here's my code in side menu. TabbarController *tabClassObj = [TabbarController sharedInstance]; [tabClassObj.selectedViewController pushViewController:vcIdentifier animated:YES]; – Human Feb 18 '20 at 11:51
  • @Sivagami does it crash or nothing happens? Try to give as much info as possible to “not working”. Put a breakpoint to the second line of code you posted. Then in console in your Xcode (usually found on bottom right) enter “po tabClassObj” and “po tabClassObj.selectedViewController” and let me know what the console prints back for both. – Matic Oblak Feb 18 '20 at 11:55
  • It's not pushing to next view controller. I check in log, tabClassObj.selectedViewController has value(Navigation controller) Please check this link, this is what am trying to achieve. https://stackoverflow.com/questions/60278538/show-tabbar-from-refrostedviewcontroller-sidemenu-objective-c – Human Feb 18 '20 at 12:02
  • If `tabClassObj.selectedViewController` is indeed a navigation controller then this is a strange issue. Your calls should be correct and this part of the code should be working correctly. Other candidates would be (not limited to) that you are calling this code on non-main thread, that this navigation controller is not on top at that time (some other controller is presented over it or other tab bar is shown), that the controller is instantly popped, that `controller` is null... – Matic Oblak Feb 18 '20 at 12:07
-1

You must push in TabBarController

self.tabBarController?.pushViewController(controller, animated: true)