0

I need advice in situation: I have several JSON where may be several types of transport are (for example, only train (first variant) or train and bus (second variant). I know that there are only 3 types of transport maximum.

So, I'd like to show info from JSON about train in first view controller, info from JSON about bus in second end etc.

How better to do: create several view controllers (for maximum variants - 3), several tabBar.items (3) and when I get data from JSON in AppDelegate I will know: "OK, I know that in that JSON info only about train and I should show only tabBar.item = "train" and work only with TrainViewController and others tabBar.items I must hide from user? Is it good experience?

Vadim Nikolaev
  • 2,132
  • 17
  • 34

2 Answers2

2

I would go about your tab bar programmatically. I would create a new Cocoa Touch Class, calling it something like CustomTabBarController and subclassing it as a UITabBarController. Go ahead to your App Delegate file and inside of your didFinishLaunchingWithOptions function, add the following:

window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
        window?.rootViewController = CustomTabBarController()

Now, when your app launches, your rootViewController will be this tab bar view. Now inside of your viewDidLoad in your CustomTabBarController class, you'd simple implement your viewControllers in an array, which your tab bar will show and go to when touched like so:

let trainController = UIViewController()
        let trainNavigationController = UINavigationController(rootViewController: trainController)
        trainNavigationController.tabBarItem.image = UIImage(named: "your_tab_icon")?.withRenderingMode(.alwaysOriginal)
        trainNavigationController.tabBarItem.selectedImage = UIImage(named: "your_tab_selected_icon")?.withRenderingMode(.alwaysOriginal)

let busController = UIViewController()
    let busNavigationController = UINavigationController(rootViewController: trainController)
    busNavigationController.tabBarItem.image = UIImage(named: "your_tab_icon")?.withRenderingMode(.alwaysOriginal)
    busNavigationController.tabBarItem.selectedImage = UIImage(named: "your_tab_selected_icon")?.withRenderingMode(.alwaysOriginal)

viewControllers = [trainNavigationController, busNavigationController]

As for the JSON part, that's a totally different ball game. There are many tutorials online and on SO. Hope this helps and points you in the right direction. Good luck!

Daniel Dramond
  • 1,538
  • 2
  • 15
  • 26
2

Your question will have multiple solutions to achieve your goal and totally depends on what kind of UI will attract your users. But along with the UI I will also advice you to consider the app size and code complexity.

If I would have to do this, I would have done like this:

1) Use single `ViewControlller` with `SegementedControl` on top having titles of your variants.

2) Whenever user selects the `segment`,load necessary data for that variant.

3) If you are going to show that data in list format, then It would be very easy to manage your `datasource` as you can simply replace the datasource depending on the selected variant.

This is not the exact or the only solution, but IMO this will reduce app size as you will be using single ViewController instead of three and you can easily manage all your complexity in a single ViewController class.

enter image description here

Vishal Sonawane
  • 2,637
  • 2
  • 16
  • 21
  • thanks, but if will be case, when I get JSON with only one type, is it good idea to hide Segmented Controls fully? – Vadim Nikolaev Dec 06 '16 at 13:14
  • No its not good hide to hide segmented control. In this case you can just pass the type of your variant to the second VC and load required data. Your ultimate goal should be loading and showing data based on type of variant. Still you can manage this with single view controller. – Vishal Sonawane Dec 06 '16 at 13:56
  • yes, I chose your variant and it's really good working with my idea, great! – Vadim Nikolaev Dec 06 '16 at 15:33
  • Pleasure to help you @VadimNikolaev. If my answer solved your problem, you can mark it as correct. Happy Coding..!! – Vishal Sonawane Dec 07 '16 at 05:21