Tag Archives: UIViewController

How to create a View Controller defined in you your Storyboard programmatically

Your View Controllers are created by the Storyboard automatically depending their defined relationships in Interface Builder.

Sometimes however we need to create and transition to View Controllers we’ve defined in code. For example, if you want to transition to a view as part of displaying a search result.

We can do this by creating a new UIStoryboard object and then asking it to create a View Controller defined in it. For this to work you need to give your View Controller a unique identifier using the Identity Inspector (under Identity, set a Storyboard ID).

This example assumes we have a Storyboard file called MainStoryboard.storyboard in which there’s a View Controller called DetailView. Here’s how we create it and push it onto a stack of Navigation Controllers:

Note that you define the Storyboard without its extension. If the bundle parameter is nil (as it is here) then the compiler assumes your main bundle.

How to pass data from the App Delegate to your Top View Controller

If the App Delegate has something that your top view controller needs then you simply pass it the required object via a property set on the top view controller. This is easy when your top view controller is also the root view controller.

However, when you embed your top view controller in a Navigation Controller, and perhaps that one is also embedded in a Tab Bar Controller, then this array isn’t quite so easy to figure out (and I must admit that I always forget how to do this when a new challenge arises).

So here’s how this works. In this example, the thing that’s displayed is called MyViewController, which is embedded in a Navigation Controller. I’m passing it the App Delegate’s self.managedObjectContext which is defined as a property on MyViewController.

We call this in the applicationDidFinishLaunching method, just before the return statement.

How to share data between Navigation Controllers

The Root Navigation Controller can serve as a data model. Each View Controller connected to the Navigation Controller via push segue can access its properties like so:

Here’s an example. MyNavController is the class for the Navigation Controller. This snipped is called from any View Controller in sequence and assumes we have an integer myNumber which is increased by one:

The same principle works for Tab Bar Controllers too.

How to trigger a Modal Segue

Once you’ve connected two View Controllers via a Segue in the storyboard you need to give it an identifier so we can call it in code like so:

Dismiss it via an action like this:

How to exchange data between View Controllers

If you have two view controller classes you probably want to exchange data between them. Thanks to two methods we can do that, provided one view controller is presented via a modal segue. Here’s how it works step by step.

Imagine you had two labels on each View Controller. We’ll call them myLabel1 and myLabel2 (1is on ViewController, 2 on MyViewController). Both View Controllers need to be connected as described previously via a segue. You can access properties on ViewController from MyViewController like so:

Likewise, if you’re in MyViewController you can access properties from ViewController like so:

How to add a second View Controller

Here’s how you add a new View Controller to your project and switch to it via a segue:

  1. open a new Single View project
  2. add am Objective-C class and select subclass of UIViewController (we’ll call it MyViewController here)
  3. in MyViewController.h, import the other ViewController.h
  4. and vice versa, in ViewController.h, import MyVewController.h too
  5. head over to the storyboard and add a ViewController
  6. in the Identity Inspector select MyViewController as a custom class (it’s set to UIViewController by default)
  7. add a segue from ViewController to MyViewController
  8. give the segue a label (let’s call it toMyView)

So far so good. Now you can kick-off a segue between them like so:

Once you’re done with the MyViewController you can dismiss it and go back to ViewController like so:

Next let’s look at how we can exchange data between both View Controllers.