Give Your App a Brain With a Flow Controller

One of the first rules a developer learns is to separate the code handling the data and the user interface. The link between the data and UI is usually done in a controller, or, in iOS, a view controller. In most projects, view controllers are interconnected and can be very dependent with one another. That’s where the flow controller comes in. It serves as a link between view controllers. It’s the brain of the app.

Technically speaking, a flow controller is a NSObject, and is very specific to the app you’re working on. Therefore it’s hard to have a base object to subclass. However, there are some rules to follow when architecting the app.

  1. Keep the application’s stateThe flow controller should always know what view controller is currently in charge. It should also poll the model objects to know the current context (feature locked/unlocked, logged in/out…).
  2. Manage the logic between view controllersThe flow controller should control the view controllers. Because it knows the application’s state, it knows when to present or dismiss a specific view controller, and which view controller should get the focus next. View controllers report to the flow controller through delegate methods so it can perform the necessary action.
  3. Orchestrate the view controllersThe flow controller should take care of the transitions and animations between view controllers. Centralizing this in an object makes it easier to replace a custom transition or change the app’s architecture (tab bar, navigation controller…).
  4. Prevent view controller dependenciesThe flow controller should remove dependencies between view controllers. They report to the flow controller when they require an external action and don’t know about each other, the flow controller does.
  5. Handle a portion of the applicationFor complex applications with a lot of view controllers, it may be better to use several flow controllers, each taking care of a different portion of the app and reporting to a master one. An application with a tab bar could have a flow controller per tab, and a main one that links them together.

I’ve used a flow controller in two projects so far, and it helped a lot organize and keep track of the workflow of the apps.

This post is based on a presentation I made at CocoaHeads Paris on January 9th, 2014. You can view the slides here.

Download a sample project here.