#2 iOS — Reinventing view controller navigation

Passing data between view controllers.

Previous posts:

CoreNavigation extends UIKit with additional features and tools helping you handle navigation use cases in few lines of code.

In this post, I’ll walk you through CoreNavigation’s Destination type and data passing capabilities. Source code can be found on GitHub.

We are gonna make a simple app with just one view controller containing a color picker.

ColorViewController can:

  • navigate to the new view controller of same type and pass UIColor instance
  • receive UIColor which will be assigned to view’s backgroundColor

View controller

ChromaColorPicker is used for the demo purpose.

Make view controller able to receive data by conforming DataReceivable protocol:

DataReceivable type has it’s associated type DataType. This means a Destination type can know about its view controller’s data type which opens a possibility to pass a concrete data from an outside world.

Destination (view controller wrapper)

Destination protocol has it’s associated type ViewControllerType which is theUIViewController type. This means destination type can know about its view controller type. In this particular case, aColor destination is paired with ColorViewController.


Detect color picker changes and navigate:

Using Destination type is optional and practically has no use in this use case but using it will give us an advantage in our next task where we’re going to extend current app with deep linking capabilities.

Happy coding!