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:

[self performSegueWithIdentifier:@"toMyView" sender:self];

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

[self dismissViewControllerAnimated:YES completion:nil];

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





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.