skip to content
Ryan Wu
Table of Contents

In iOS development we use a ViewController to own the behavior of each screen.

The first problem is usually this:

“How do I pass information between two ViewControllers?”

Here are a few common ways.

1. Pass information through the instance

Every ViewController class, whether it comes from a Storyboard or a nib, has to be initialized (alloc + init) before you can work with the instance. To pass information between them, you need those instances.

When the payload is simple, or you only pass it once, going through the object instance is the simpler path.

Example A: from a nib, get the ViewController2 instance and set user

Example B: in a Storyboard, use the instance from the segue and set user

2. Pass information through a singleton

Passing through an instance is convenient, but shared information often lives beyond two ViewControllers. The app author’s info, or the current user’s cached data, belongs in one common place that everyone reads and writes. A singleton makes that simpler.

Singleton

Sample code: