I am writing an application which has a TabBar based navigation. I am adopting the VIPER architecture but I am really confused with the topic of how a UITabBarController's tab changing should be implemented.
This might be late, but it might be helpful for others. My use case was to implement the tabBarController after the login screen. There can be many ways we can do it in VIPER but how I did it is as follows:
Hope I made sense.
Another way to implement UITabBarController
with VIPER architecture is to provide a TabBarInterface
import Foundation
import UIKit
protocol TabBarInterface {
func configuredViewController() -> UIViewController
}
So that each wireframe that presents a view controller in the tab bar controller implements TabBarInterface
and then installIntoWindow
just loops through all the wireframes calling configuredViewController
for each wireframe it will present.
import Foundation
import UIKit
class TabBarWireframe : NSObject {
let wireFrames:[TabBarInterface]
var rootWireframe : RootWireframe?
init(_ wireFrames:TabBarInterface...) {
self.wireFrames = wireFrames
super.init()
}
private override init() {
self.wireFrames = [TabBarInterface]()
}
func installIntoWindow(window: UIWindow) {
let tabBarController = MainTabBarController()
var viewControllers = [UIViewController]()
for wireFrame in wireFrames {
viewControllers.append(wireFrame.configuredViewController())
}
tabBarController.viewControllers = viewControllers
tabBarController.navigationItem.title = "Visva"
self.rootWireframe?.installTabBarControllerIntoWindow(tabBarController: tabBarController, window: window)
}
}
Please note that in our case RootWireframe
installs the tab bar controller into the main Window, i.e:
window.rootViewController = tabBarController
window.makeKeyAndVisible()
I'm still new to VIPER so my two cents may not be worth much but maybe have the tabbar as a private property on the AppDelegate. When you need to change to a particular index have utility methods that change the tabbar selected index but also trigger the wireframe/router creation process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With