View presentation and segues

When I started developing iOS applications, I really didn’t know where to start and what is the best way to do things like this. So, what was the main problem? When I started my first project I wanted to connect a two view controllers but I didn’t know how. While I was searching the web, I found many different answers to this problem, so there were many ways to do this and I didn’t know which one to use.

After I’ve done some exercises, I was able to do this on many different ways. So, in this post I will show you three ways for presenting a view controller: Code only, Code & Segue and Segue.

There is another thing I want to say before getting started. I will show a simple examples without using the navigation controller. Navigation controller can make this even simpler because it can create different animations and buttons for going back, which we will create manually in examples below.

Presenting view controller with code only

This is the first example where I will show you how to create connection between two view controllers. First of all, I need to create two view controllers (FirstViewController.swift and SecondViewController.swift) and attach them to a view controllers in the storyboard.

Post - View presentation and segues - Image 1

After that, I will connect outlets and actions from the storyboard to a view controllers. This can be seen on the images below.

Post – View presentation and segues – Image 2

After I did that, there is still the problem. Those two view controller aren’t connected. After I connect them, I’ll pass a random number from the FirstViewController to a SecondViewController to prove that the view controllers are connected in this examples.

Before getting into the code, there is only one thing I need to do to make this work. In the code I will use a method which needs to know a view controller ID, so I need to add an ID to the SecondViewController. In the image you can se how to do that.

Post – View presentation and segues – Image 4

After I did a storyboard preparation, I will explain the code which runs an application. Here is the code for the FirstViewController:

As I already mentioned, I connected a label for the random number and an action for button which will take me to the SecondViewController. On a line 15 is generated the random number, and in method viewWillAppear is that number set as text in a label. If user click on the button, action showSecondViewController will be executed. This method gets a view controller from the storyboard but what really matters is a cast to SecondViewController. After that, only thing to do is to assign a random number from the current view controller to destination view controller. To show the SecondViewController it needs to be called method presentViewController.

Now, the SecondViewController is shown. But, how did I managed to display a random number on the view?

As you can see, in the method viewWillAppear I’m assigning a number to a numberLabel. Also, for removing this view controller and going back to the FirstViewController there is an action goBack which executes the method dismissViewController.

Presenting view controller with code and segue

In this example I’ll change the existing code and add more things in storyboard. First, I need to add segue in storyboard which will connect FirstViewController and SecondViewController. In this case, I will create custom segue with an identifier (very important) and use modal segue for showing the second view controller.

Post - View presentation and segues - Image 5

Now, lets go to the FirstViewController and remove unnecessary code. After that I need to call method performSegueWithIdentifier inside action showSecondViewController which will be executed after user tap on the button. This method will call segue which will show SecondViewController.

But, isn’t something missing? What about the passing our random number value? How to manage that? I need to override method prepareForSegue which is executed every time before the segue is executed. In this method I need to pass a value from current view controller to the destination view controller.

Please note that the SecondViewController’s code remains unchanged.

Presenting view controller with segue only

The last example is using only the segue, without any code to call segue to be executed. I will do only small changes to make this work. So, first thing to do is to remove existing custom segue between view controllers in the storyboard. After that I’ll create a new segue with identifier. Segue can be created without an identifier, but with an identifier assigned to the segue it’s very easy to handle multiple segues connected to one view controller. In the image below you can see how to do that. This segue will be executed every time user taps the button. Also, I need to remove a connection between the button and the action in FirstViewController.

Post - View presentation and segues - Image 6

Now, the last thing to do is to change code in a FirstViewController. The code is really short, but everything will work great. The storyboard will call a segue, and in the code, only thing I need to do is to implement a method prepareForSegue where a random number will be passed to the destination view controller

In the image below you can see how this example works:

Post - View presentation and segues - Image 7