Next Level Swift

Next Level Swift aims at sharing knowledge and insights into better programming for iOS and is dedicated to help developers bring their apps to the next level!

Follow publication

UITabBarController Programmatically

Eric Giannini
Next Level Swift
Published in
1 min readMay 1, 2017

--

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.

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in Next Level Swift

Next Level Swift aims at sharing knowledge and insights into better programming for iOS and is dedicated to help developers bring their apps to the next level!

Written by Eric Giannini

🙌 Working with Swift, Kotlin, the world wide web

No responses yet

Write a response