UIViewControllers are among the most used UIKit objects. As the name implies UIViewControllers are controller objects and their purpose should be to populate data to Views and to react to View actions. They have so many capabilities that often tend to grow in size which leads to massive view controllers, a common code smell in iOS codebases. We should always try to keep them very simple and use separate object for business logic and presentation logic. It should be so simple that unit testing should be trivial on this classes. But sometimes as part of their duty to populate data to the views they might need to use an other class, a collaborator. Clean code practises recommend those collaborators to be injected into the Class, preferably with the constructor dependency injection technique.
In this post we will learn how to use the constructor dependency injection technique when we are using UIViewControllers with Nib files. To keep the example simple and straight to the point we will to try to use a string as the dependency. And the project will have only two UIViewControllers, the initial and the one we want to initialise with the dependency.
The first ViewController has a view with just a button
and it’s code is as simple as :
1 2 3 4 5 6 7 8 9 |
import UIKit class ViewController: UIViewController { @IBAction func didTapPresentAnotherViewController(_ sender: Any) { } } |
The second ViewController’s view has one button and one label, when the user taps on the button the label should show the value of the string that we should pass as a dependency:
And it’s code is as follows:
1 2 3 4 5 6 7 8 9 10 |
import UIKit final class AnotherViewController: UIViewController { @IBOutlet weak var aDependenceLabel: UILabel! @IBAction func didTapShow(_ sender: Any) { } } |
All we have to do in order to use the constructor dependency injection technique to pass the dependency into the AnotherViewController class is to use a custom initialiser:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import UIKit final class AnotherViewController: UIViewController { @IBOutlet weak var aDependenceLabel: UILabel! private let aDependency: String init(aDependency: String) { self.aDependency = aDependency super.init(nibName: nil, bundle: nil) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } @IBAction func didTapShow(_ sender: Any) { aDependenceLabel.text = aDependency } } |
We notice that at the end of our new initialiser we have to call the init of the super method to make sure that the ViewController it initialises properly.
Now when we want to present the AnotherViewController from our first ViewController all we need to do is:
1 2 3 4 5 6 7 8 9 |
import UIKit class ViewController: UIViewController { @IBAction func didTapPresentAnotherViewController(_ sender: Any) { let anotherViewController = AnotherViewController(aDependency: "a String") present(anotherViewController, animated: true, completion: nil) } } |
Run the app, tap to show the AnotherViewController and tap on the button: