Trikalabs
  • Home
  • Best online TDD videos
  • Book Suggestions
  • About Me
  • Contact
Trikalabs
No Result
View All Result

Dependency injection – UIViewController in Storyboard (iOS 13)

by fragi
May 13, 2021
in Unit Testing
Share on FacebookShare on Twitter

Finally Apple (iOS 13 +) has added constructor dependency injection to Storyboards!

No excuses anymore for not using DI properly on UIViewControllers.

Let’s see a quick example.

We have an app with a simple Storyboard with two UIViewControllers.

The code for the second UIViewController is as follows:

Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
final class AnotherViewController: UIViewController {
    
    private let aDependency: String
    @IBOutlet weak var showDependencyLabel: UILabel!
    
    init?(coder: NSCoder, aDependency: String) {
        self.aDependency = aDependency
        super.init(coder: coder)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @IBAction func didTapButton(_ sender: Any) {
        showDependencyLabel.text = aDependency
    }
}

In order to pass the dependency we have to use the failable init method. 

The code for the UIViewController that shows the AnotherViewController is:

Swift
1
2
3
4
5
6
7
8
9
10
11
12
import UIKit
 
final class ViewController: UIViewController {
    
    @IBAction func didTapButton(_ sender: Any) {
        guard let anotherViewController = storyboard?.instantiateViewController(identifier: "AnotherViewController", creator: { coder in
            return AnotherViewController(coder: coder, aDependency: "something")
        }) else { fatalError("AnotherViewController failed to be initialized")}
        
        present(anotherViewController, animated: true, completion: nil)
    }
}

As the code shows all we need to do is to use the new method 

instantiateViewController(identifier:creator:).

Tags: Dependency InjectionStoryboard
fragi

fragi

Related Posts

What is TDD?
Unit Testing

Write better Unit tests with XCTUnwrap

January 27, 2022

Testing optionals can require boilerplate code to unwrap it. It can also affect the readability of the test. Let's look...

Test static method on collaborator
Unit Testing

Test static method on collaborator

May 13, 2021

One of the main issues we are facing when we try to make an old piece of code testable is...

Unit testing code with DispatchQueue
Unit Testing

Unit testing code with DispatchQueue

May 13, 2021

One way of testing code with DispatchQueue is by using expectations (https://developer.apple.com/documentation/xctest/asynchronous_tests_and_expectations/testing_asynchronous_operations_with_expectations) but in most of the cases it's more...

Unit testing UIViewController LifeCycle
Unit Testing

Unit testing UIViewController LifeCycle

May 13, 2021

Unit testing UIViewController life cycle events are not a straightforward process. Let's have a look on the following view controller:...

Dependency Injection – UIViewController with a Nib file
Unit Testing

Dependency Injection – UIViewController with a Nib file

May 13, 2021

UIViewControllers are among the most used UIKit objects. As the name implies UIViewControllers are controller objects and their purpose should...

Unit Test Post Notification
Unit Testing

Unit Test Post Notification

July 19, 2019

In this article, we will see how we can test code that post notifications. Let's see our example code: ...

Next Post
Test static method on collaborator

Test static method on collaborator

Large Content Viewer

Large Content Viewer

Large Content Viewer

SwiftUI - Accessibility for Images

  • Advertise
  • Privacy & Policy
  • Contact

© 2019 Trikalabs Ltd. All rights reserved.

No Result
View All Result
  • Home
  • About Me
  • A curated list with the best free online TDD videos
  • Book Suggestions
  • Pinner Code Club

© 2019 Trikalabs Ltd. All rights reserved.