I have a tab bar controller which has two items connected to two view controllers(say A & B) now I want to add a 3rd view controller (say C). But I don't want to add a third icon in tab view. When the user select the second icon it should render B or C depending on whether user has signed in or not.
Asked
Active
Viewed 3,849 times
2
-
if anyone is down voting at least tell me why are you down voting? – Iqbal Inzamam Apr 16 '17 at 14:21
-
Can you add what you have done till now here? – Rahul Apr 16 '17 at 14:21
-
@IqbalInzamam: Questions usually get voted down on Stackoverflow when people seem to haven't tried to come up with a solution for themselves before asking for help. You should try to describe your thoughts for a possible approach in your question and specifically ask why this approach doesn't work or if there are better alternatives. – Mischa Apr 16 '17 at 14:32
-
And dude: If you want to get helpful answers here, at least take the time to setup your questions properly. I just fixed your image that was only linked with a title "enter image description here". Otherwise you'll get a lot more down votes and even worse: No qualified answer. – Mischa Apr 16 '17 at 17:06
1 Answers
6
Just create another view controller for the tab bar item for which you want to display different views depending on the context. Let's call it RouterViewController
. Then pass the information you need to decide which view controller to show to that RouterViewController
and implement the necessary logic there.
From the RouterViewController
you can now present whatever view controller you want (without animation). There are plenty of ways to do this, for example:
- Make your
RouterViewController
a subclass ofUINavigationController
and dynamically set itsrootViewController
property. - Make your
RouterViewController
a container view controller and embed the desired view controller accordingly. - Present a view controller modally (without animation) from your
RouterViewController
. (I personally would discourage this option because presenting modal view controllers is intended for another purpose.)
etc.

Mischa
- 15,816
- 8
- 59
- 117
-
1Thank you. I'm trying the 1st way you suggested. So I added a navigation controller as the second item of the tab controller. Then I couldn't understand where to set the rootViewController of navigation controller. Is it in the viewDidLoad of the tabBarController and how to set it? Please help me. – Iqbal Inzamam Apr 16 '17 at 15:17
-
1I suspect you're using a storyboard? If yes: 1. Add your navigation controller as the second tab of the tab bar controller. 2. Create a subclass of `UINavigationController` and call it `RouterViewController`. 3. Select the navigation controller in your storyboard and set its class to `RouterViewController`. 4. In the `RouterViewController`'s `viewDidLoad()` method set the root view controller like this: `viewControllers = [theViewControllerYouWantToShow]`. – Mischa Apr 16 '17 at 15:31
-
1this may be a very stupid question. Sorry i'm very new to ios. I did as u said, the how do I connect UINavigationController and the UIController (B & C), through which type of segue. I did the fallowing if (...){ viewControllers = [FirstViewController()] }else{ viewControllers = [SecondViewController()] } but when I click second tab icon it loads a navigation ui with black screen I have attached the story board diagram in the question – Iqbal Inzamam Apr 16 '17 at 16:42
-
1You don't do it with segues, you do it in code. However, if you simply instantiate an instance of your respective view controllers (`FirstViewController()`) you'll get a view controller with an empty (black) view because it's not linked to the storyboard in any way. In order to link it you need to get a reference to your storyboard in code and then call [instantiateViewController(withIdentifier:)](https://developer.apple.com/reference/uikit/uistoryboard/1616214-instantiateviewcontroller) on it. Just google "instantiate view controller from storyboard" and you'll find a solution. – Mischa Apr 16 '17 at 16:58