how to connect multiple views to a single tab bar

2019-04-12 17:09发布

问题:

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.

回答1:

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 of UINavigationController and dynamically set its rootViewController 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.