UIViewController Archives

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 […]

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, […]

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: ((MyNavController *)self.parentViewController).mySourceProperty 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 […]

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: [self performSegueWithIdentifier:@”showDetail” sender:self]; Dismiss it via an action like this: [self dismissViewControllerAnimated:YES completion:nil];

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 […]

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: open a new Single View project add am Objective-C class and select subclass of UIViewController (we’ll call it MyViewController here) in MyViewController.h, import the other ViewController.h and vice versa, in ViewController.h, import MyVewController.h too head over to […]