UITabBarController Programmatically
While AppDelegate.swift
looks like a great place to program the instantiation of an instance of UITabBarController programmatically, the AppDelegate.swift
is monadic. There is no reason to add functionality to the delegate, since the delegate’s function is already singularly defined.
Programming the AppDelegate.swift
as though the AppDelegate.swift
were a UITabBarController leads to anomalies. Imagine, for instance, requiring the AppDelegate.swift
to conform to UITabBarControllerDelegate
. Accordingly, implementing the UITabBarControllerDelegate
's didSelect
method in the AppDelegate.swift
requires the AppDelegate.swift
to initialize instances of each of the view controllers on the UITabBarController
's tab. It does not make sense to treat the AppDelegate.swift
as though AppDelegate.swift
were a UITabBarController, since none of these aspects of delegation are an extension of its own functionality. Since the AppDelegate.swift
is monadic, these aspects of delegation ought to be completely modulated.
A modular implementation of a programmatic instantiation of UITabBarController is to create a separate ViewController whose extension conforms to UITabBarController. As opposed to creating instances of the view controllers for its tabs, these instances may be lazily initialized so that neither of the tabs is created until a tab be selected.
The next step is set an instance of UITabBarController programmatically as the window
's rootViewController
in the AppDelegate
's method calleddidFinishLaunchingWithOptions
.
self.window = UIWindow(frame: UIScreen.main.bounds)let tabBarController = TabBarController()self.window?.rootViewController = tabBarControllerwindow?.backgroundColor = UIColor.whiteself.window?.makeKeyAndVisible()
The AppDelegate.swift
is thus impervious to the internal structure of the UITabBarController, since the internal structure of the UITabBarController is modulated into its own structure. These separations preserve the structural integrity of both implementations without any crossover.