Personaly I like the idea behind UIStoryboardSegue of decomposition navigation logic from your business logic routine.
But I’m not a fun of using it with storyboard and I’ve never created them programmatically.
So decided to adopt it feature and create something similar.
But what fields this type should have?
It definitely should have
let fromViewController:UIViewController
Most of us passing some context (or not) which we want to display on the next ViewController.
Because swift have such a cool feature called Generics lets take advantage of this and make our context to be a generic type T
Also, we some how need to create toViewController object. I decided to do this with block, lets call it
let toViewControllerFactory:(context:T) -> UIViewController
And now we ready to implement “reactive” part of our segues =)
There are two things that segue could do — it could push new viewController or present it modally.
private(set) lazy var pushObserver:AnyObserver<T>private(set) lazy var presentObserver:AnyObserver<T>
So lets implement these observers
Note: If you don’t want to pass the context within segue just create it with Void type
lazy var segue:Segue<Void>
And now we can user our Segue some thing like this:
What it gives? It separate your navigation logic, and also this class could be easily covered with unit test.
Ideas, remarks? Will be happy to discuss in comments ;)